mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-24 19:12:33 +00:00

Update to [Rust 1.74](https://blog.rust-lang.org/2023/11/16/Rust-1.74.0.html) and use the new clippy lints table. The update itself introduced a new clippy lint about superfluous hashes in raw strings, which got removed. I moved our lint config from `rustflags` to the newly stabilized [workspace.lints](https://doc.rust-lang.org/stable/cargo/reference/workspaces.html#the-lints-table). One consequence is that we have to `unsafe_code = "warn"` instead of "forbid" because the latter now actually bans unsafe code: ``` error[E0453]: allow(unsafe_code) incompatible with previous forbid --> crates/ruff_source_file/src/newlines.rs:62:17 | 62 | #[allow(unsafe_code)] | ^^^^^^^^^^^ overruled by previous forbid | = note: `forbid` lint level was set on command line ``` --------- Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
143 lines
3.9 KiB
Rust
143 lines
3.9 KiB
Rust
use ruff_python_ast::parenthesize::parenthesized_range;
|
|
use ruff_python_parser::parse_expression;
|
|
use ruff_python_trivia::CommentRanges;
|
|
use ruff_text_size::TextRange;
|
|
|
|
#[test]
|
|
fn test_parenthesized_name() {
|
|
let source_code = r"(x) + 1";
|
|
let expr = parse_expression(source_code, "<filename>").unwrap();
|
|
|
|
let bin_op = expr.as_bin_op_expr().unwrap();
|
|
let name = bin_op.left.as_ref();
|
|
|
|
let parenthesized = parenthesized_range(
|
|
name.into(),
|
|
bin_op.into(),
|
|
&CommentRanges::default(),
|
|
source_code,
|
|
);
|
|
assert_eq!(parenthesized, Some(TextRange::new(0.into(), 3.into())));
|
|
}
|
|
|
|
#[test]
|
|
fn test_non_parenthesized_name() {
|
|
let source_code = r"x + 1";
|
|
let expr = parse_expression(source_code, "<filename>").unwrap();
|
|
|
|
let bin_op = expr.as_bin_op_expr().unwrap();
|
|
let name = bin_op.left.as_ref();
|
|
|
|
let parenthesized = parenthesized_range(
|
|
name.into(),
|
|
bin_op.into(),
|
|
&CommentRanges::default(),
|
|
source_code,
|
|
);
|
|
assert_eq!(parenthesized, None);
|
|
}
|
|
|
|
#[test]
|
|
fn test_parenthesized_argument() {
|
|
let source_code = r"f((a))";
|
|
let expr = parse_expression(source_code, "<filename>").unwrap();
|
|
|
|
let call = expr.as_call_expr().unwrap();
|
|
let arguments = &call.arguments;
|
|
let argument = arguments.args.first().unwrap();
|
|
|
|
let parenthesized = parenthesized_range(
|
|
argument.into(),
|
|
arguments.into(),
|
|
&CommentRanges::default(),
|
|
source_code,
|
|
);
|
|
assert_eq!(parenthesized, Some(TextRange::new(2.into(), 5.into())));
|
|
}
|
|
|
|
#[test]
|
|
fn test_non_parenthesized_argument() {
|
|
let source_code = r"f(a)";
|
|
let expr = parse_expression(source_code, "<filename>").unwrap();
|
|
|
|
let call = expr.as_call_expr().unwrap();
|
|
let arguments = &call.arguments;
|
|
let argument = arguments.args.first().unwrap();
|
|
|
|
let parenthesized = parenthesized_range(
|
|
argument.into(),
|
|
arguments.into(),
|
|
&CommentRanges::default(),
|
|
source_code,
|
|
);
|
|
assert_eq!(parenthesized, None);
|
|
}
|
|
|
|
#[test]
|
|
fn test_parenthesized_tuple_member() {
|
|
let source_code = r"(a, (b))";
|
|
let expr = parse_expression(source_code, "<filename>").unwrap();
|
|
|
|
let tuple = expr.as_tuple_expr().unwrap();
|
|
let member = tuple.elts.last().unwrap();
|
|
|
|
let parenthesized = parenthesized_range(
|
|
member.into(),
|
|
tuple.into(),
|
|
&CommentRanges::default(),
|
|
source_code,
|
|
);
|
|
assert_eq!(parenthesized, Some(TextRange::new(4.into(), 7.into())));
|
|
}
|
|
|
|
#[test]
|
|
fn test_non_parenthesized_tuple_member() {
|
|
let source_code = r"(a, b)";
|
|
let expr = parse_expression(source_code, "<filename>").unwrap();
|
|
|
|
let tuple = expr.as_tuple_expr().unwrap();
|
|
let member = tuple.elts.last().unwrap();
|
|
|
|
let parenthesized = parenthesized_range(
|
|
member.into(),
|
|
tuple.into(),
|
|
&CommentRanges::default(),
|
|
source_code,
|
|
);
|
|
assert_eq!(parenthesized, None);
|
|
}
|
|
|
|
#[test]
|
|
fn test_twice_parenthesized_name() {
|
|
let source_code = r"((x)) + 1";
|
|
let expr = parse_expression(source_code, "<filename>").unwrap();
|
|
|
|
let bin_op = expr.as_bin_op_expr().unwrap();
|
|
let name = bin_op.left.as_ref();
|
|
|
|
let parenthesized = parenthesized_range(
|
|
name.into(),
|
|
bin_op.into(),
|
|
&CommentRanges::default(),
|
|
source_code,
|
|
);
|
|
assert_eq!(parenthesized, Some(TextRange::new(0.into(), 5.into())));
|
|
}
|
|
|
|
#[test]
|
|
fn test_twice_parenthesized_argument() {
|
|
let source_code = r"f(((a + 1)))";
|
|
let expr = parse_expression(source_code, "<filename>").unwrap();
|
|
|
|
let call = expr.as_call_expr().unwrap();
|
|
let arguments = &call.arguments;
|
|
let argument = arguments.args.first().unwrap();
|
|
|
|
let parenthesized = parenthesized_range(
|
|
argument.into(),
|
|
arguments.into(),
|
|
&CommentRanges::default(),
|
|
source_code,
|
|
);
|
|
assert_eq!(parenthesized, Some(TextRange::new(2.into(), 11.into())));
|
|
}
|