Skip to content

Commit a9d0a6f

Browse files
committed
Auto merge of #138271 - mu001999-contrib:fix-138234, r=jackh726
Keep space if arg does not follow punctuation when lint unused parens Fixes #138234 If the arg follows punctuation, still pass `left_pos` with `None` and no space will be added, else then pass `left_pos` with `Some(arg.span.lo())`, so that we can add the space as expected. And `emit_unused_delims` can make sure no more space will be added if the expr follows space. --- Edited: Directly use the `value_span` to check whether the expr removed parens will follow identifier or be followed by identifier.
2 parents eec6bd9 + 9991ec2 commit a9d0a6f

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

compiler/rustc_lint/src/unused.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,10 @@ trait UnusedDelimLint {
843843
&& !snip.ends_with(' ')
844844
{
845845
" "
846+
} else if let Ok(snip) = sm.span_to_prev_source(value_span)
847+
&& snip.ends_with(|c: char| c.is_alphanumeric())
848+
{
849+
" "
846850
} else {
847851
""
848852
};
@@ -852,6 +856,10 @@ trait UnusedDelimLint {
852856
&& !snip.starts_with(' ')
853857
{
854858
" "
859+
} else if let Ok(snip) = sm.span_to_prev_source(value_span)
860+
&& snip.starts_with(|c: char| c.is_alphanumeric())
861+
{
862+
" "
855863
} else {
856864
""
857865
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ run-rustfix
2+
3+
#![deny(unused_parens)]
4+
5+
macro_rules! wrap {
6+
($name:ident $arg:expr) => {
7+
$name($arg);
8+
};
9+
}
10+
11+
fn main() {
12+
wrap!(unary routine()); //~ ERROR unnecessary parentheses around function argument
13+
wrap!(unary routine()); //~ ERROR unnecessary parentheses around function argument
14+
}
15+
16+
fn unary(_: ()) {}
17+
fn routine() {}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ run-rustfix
2+
3+
#![deny(unused_parens)]
4+
5+
macro_rules! wrap {
6+
($name:ident $arg:expr) => {
7+
$name($arg);
8+
};
9+
}
10+
11+
fn main() {
12+
wrap!(unary(routine())); //~ ERROR unnecessary parentheses around function argument
13+
wrap!(unary (routine())); //~ ERROR unnecessary parentheses around function argument
14+
}
15+
16+
fn unary(_: ()) {}
17+
fn routine() {}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error: unnecessary parentheses around function argument
2+
--> $DIR/unused_parens_follow_ident.rs:12:16
3+
|
4+
LL | wrap!(unary(routine()));
5+
| ^ ^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/unused_parens_follow_ident.rs:3:9
9+
|
10+
LL | #![deny(unused_parens)]
11+
| ^^^^^^^^^^^^^
12+
help: remove these parentheses
13+
|
14+
LL - wrap!(unary(routine()));
15+
LL + wrap!(unary routine());
16+
|
17+
18+
error: unnecessary parentheses around function argument
19+
--> $DIR/unused_parens_follow_ident.rs:13:17
20+
|
21+
LL | wrap!(unary (routine()));
22+
| ^ ^
23+
|
24+
help: remove these parentheses
25+
|
26+
LL - wrap!(unary (routine()));
27+
LL + wrap!(unary routine());
28+
|
29+
30+
error: aborting due to 2 previous errors
31+

0 commit comments

Comments
 (0)