-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rustc says necessary parentheses are unnecessary #120642
Labels
A-lint
Area: Lints (warnings about flaws in source code) such as unused_mut.
A-suggestion-diagnostics
Area: Suggestions generated by the compiler applied by `cargo fix`
C-bug
Category: This is a bug.
D-invalid-suggestion
Diagnostics: A structured suggestion resulting in incorrect code.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Comments
rustbot
added
the
needs-triage
This issue may need triage. Remove it if it has been sufficiently triaged.
label
Feb 4, 2024
clubby789
added
A-diagnostics
Area: Messages for errors, warnings, and lints
D-incorrect
Diagnostics: A diagnostic that is giving misleading or incorrect information.
and removed
needs-triage
This issue may need triage. Remove it if it has been sufficiently triaged.
labels
Feb 4, 2024
fmease
added
A-lint
Area: Lints (warnings about flaws in source code) such as unused_mut.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
A-suggestion-diagnostics
Area: Suggestions generated by the compiler applied by `cargo fix`
D-invalid-suggestion
Diagnostics: A structured suggestion resulting in incorrect code.
and removed
A-diagnostics
Area: Messages for errors, warnings, and lints
D-incorrect
Diagnostics: A diagnostic that is giving misleading or incorrect information.
labels
Feb 4, 2024
surechen
added a commit
to surechen/rust
that referenced
this issue
Apr 1, 2024
surechen
added a commit
to surechen/rust
that referenced
this issue
Apr 10, 2024
fmease
added a commit
to fmease/rust
that referenced
this issue
Apr 10, 2024
Skip `unused_parens` report for `Paren(Path(..))` in macro. fixes rust-lang#120642 In following code, `unused_parens` suggest change `<($($rest),*)>::bar()` to `<$rest>::bar()` which will cause another err: `error: variable 'rest' is still repeating at this depth`: ```rust trait Foo { fn bar(); } macro_rules! problem { ($ty:ident) => { impl<$ty: Foo> Foo for ($ty,) { fn bar() { <$ty>::bar() } } }; ($ty:ident $(, $rest:ident)*) => { impl<$ty: Foo, $($rest: Foo),*> Foo for ($ty, $($rest),*) { fn bar() { <$ty>::bar(); <($($rest),*)>::bar() } } problem!($($rest),*); } } ``` I think maybe we can handle this by avoid warning for `Paren(Path(..))` in the macro. Is this reasonable approach?
fmease
added a commit
to fmease/rust
that referenced
this issue
Apr 10, 2024
Skip `unused_parens` report for `Paren(Path(..))` in macro. fixes rust-lang#120642 In following code, `unused_parens` suggest change `<($($rest),*)>::bar()` to `<$rest>::bar()` which will cause another err: `error: variable 'rest' is still repeating at this depth`: ```rust trait Foo { fn bar(); } macro_rules! problem { ($ty:ident) => { impl<$ty: Foo> Foo for ($ty,) { fn bar() { <$ty>::bar() } } }; ($ty:ident $(, $rest:ident)*) => { impl<$ty: Foo, $($rest: Foo),*> Foo for ($ty, $($rest),*) { fn bar() { <$ty>::bar(); <($($rest),*)>::bar() } } problem!($($rest),*); } } ``` I think maybe we can handle this by avoid warning for `Paren(Path(..))` in the macro. Is this reasonable approach?
fmease
added a commit
to fmease/rust
that referenced
this issue
Apr 10, 2024
Skip `unused_parens` report for `Paren(Path(..))` in macro. fixes rust-lang#120642 In following code, `unused_parens` suggest change `<($($rest),*)>::bar()` to `<$rest>::bar()` which will cause another err: `error: variable 'rest' is still repeating at this depth`: ```rust trait Foo { fn bar(); } macro_rules! problem { ($ty:ident) => { impl<$ty: Foo> Foo for ($ty,) { fn bar() { <$ty>::bar() } } }; ($ty:ident $(, $rest:ident)*) => { impl<$ty: Foo, $($rest: Foo),*> Foo for ($ty, $($rest),*) { fn bar() { <$ty>::bar(); <($($rest),*)>::bar() } } problem!($($rest),*); } } ``` I think maybe we can handle this by avoid warning for `Paren(Path(..))` in the macro. Is this reasonable approach?
rust-timer
added a commit
to rust-lang-ci/rust
that referenced
this issue
Apr 11, 2024
Rollup merge of rust-lang#123314 - surechen:fix_120642, r=Nadrieril Skip `unused_parens` report for `Paren(Path(..))` in macro. fixes rust-lang#120642 In following code, `unused_parens` suggest change `<($($rest),*)>::bar()` to `<$rest>::bar()` which will cause another err: `error: variable 'rest' is still repeating at this depth`: ```rust trait Foo { fn bar(); } macro_rules! problem { ($ty:ident) => { impl<$ty: Foo> Foo for ($ty,) { fn bar() { <$ty>::bar() } } }; ($ty:ident $(, $rest:ident)*) => { impl<$ty: Foo, $($rest: Foo),*> Foo for ($ty, $($rest),*) { fn bar() { <$ty>::bar(); <($($rest),*)>::bar() } } problem!($($rest),*); } } ``` I think maybe we can handle this by avoid warning for `Paren(Path(..))` in the macro. Is this reasonable approach?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
A-lint
Area: Lints (warnings about flaws in source code) such as unused_mut.
A-suggestion-diagnostics
Area: Suggestions generated by the compiler applied by `cargo fix`
C-bug
Category: This is a bug.
D-invalid-suggestion
Diagnostics: A structured suggestion resulting in incorrect code.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
This code:
Playground
Creates this diagnostic:
I ran
cargo fix --lib -p playground --allow-dirty
(note that I created a local project called playground) which created this output:This told me to create an issue here.
Going into more depth regarding the issue - the macro expands after one step like so:
and we can see where the issue is:
These parentheses are indeed fully removable, and it points to an issue in the macro definition. By changing
to
the compiler no longer emits the warning, because now it expands to
which is properly recognized as a tuple with one element, rather than a type surrounded by redundant parentheses.
Another way to fix it is to remove the outer parentheses in the macro call:
which does compile without warnings, but breaks usage of the macro with tuples with more than two elements:
Playground
as while its fine to remove the parentheses when there is only one element between the angle brackets (
<(T2)>
to<T2>
), it is not fine when there are more than one element in the tuple (<(T2, T3)>
to<T2, T3>
).Importantly, the compiler still emits the unused parentheses warning and suggests their removal when the macro is called with three types when you haven't removed the parentheses (Playground).
Therefore, part of the issue is that rustc detects the problem, and assumes it can fix it by going straight to the macro and correcting it, neglecting previous expansions of the macro which require the currently redundant parentheses.
I say part of the issue, because going back to the original diagnostic:
rustc marks
($(
and),*)
as removable, and it suggests you do so:when the diagnostic should really only be suggesting the removal of the outer brackets.
I'm not too sure why this one happens. Using Syn's terminology, my best guess is that it has something to do with the span of the outer parentheses including the inner
$(
and),*
after expansion.Meta
rustc --version --verbose
:Also tried with nightly (had identical results):
The text was updated successfully, but these errors were encountered: