Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 3bb53a8

Browse files
committed
Auto merge of rust-lang#138271 - mu001999-contrib:fix-138234, r=<try>
Keep space if arg does not follow punctuation when lint unused parens Fixes rust-lang#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.
2 parents 2cd3783 + 42ce300 commit 3bb53a8

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

compiler/rustc_lint/src/unused.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,16 @@ trait UnusedDelimLint {
922922
return;
923923
}
924924
for arg in args_to_check {
925-
self.check_unused_delims_expr(cx, arg, ctx, false, None, None, false);
925+
let sm = cx.sess().source_map();
926+
let left_pos = if let Ok(snip) = sm.span_to_prev_source(arg.span)
927+
&& !snip.ends_with(|c: char| c.is_ascii_punctuation())
928+
{
929+
Some(arg.span.lo())
930+
} else {
931+
None
932+
};
933+
934+
self.check_unused_delims_expr(cx, arg, ctx, false, left_pos, None, false);
926935
}
927936
return;
928937
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![deny(unused_parens)]
2+
3+
macro_rules! wrap {
4+
($name:ident $arg:expr) => {
5+
$name($arg);
6+
};
7+
}
8+
9+
fn main() {
10+
wrap!(unary(routine())); //~ ERROR unnecessary parentheses around function argument
11+
wrap!(unary (routine())); //~ ERROR unnecessary parentheses around function argument
12+
}
13+
14+
fn unary(_: ()) {}
15+
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:10:16
3+
|
4+
LL | wrap!(unary(routine()));
5+
| ^ ^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/unused_parens_follow_ident.rs:1: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:11: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)