Skip to content
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

unused_parens triggers on macro by example code. #66295

Closed
weiznich opened this issue Nov 11, 2019 · 11 comments · Fixed by #66983
Closed

unused_parens triggers on macro by example code. #66295

weiznich opened this issue Nov 11, 2019 · 11 comments · Fixed by #66983
Assignees
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. P-high High priority regression-from-stable-to-beta Performance or correctness regression from stable to beta. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@weiznich
Copy link
Contributor

The latest beta contains a new? lunused_parens lint that triggers on macro by example code. This does not necessarily make sense because macros could be used to construct nearly arbitrary custom syntax for example something requiring to wrap a single element into a parenthesis.
The strange thing here is that seems to only happen on beta, not on nightly, not on current stable.

Found via the diesel CI

Error message:

    --> diesel/src/macros/mod.rs:274:27
     |
274  |               primary_key = (id),
     |                             ^^^^ help: remove these parentheses
…     |                             ^^^^ help: remove these parentheses
...
1085 | /     table! {
1086 | |         use sql_types::*;
1087 | |         use macros::tests::my_types::*;
1088 | |
...    |
1092 | |         }
1093 | |     }
     | |_____- in this macro invocation

rustc version: rustc 1.40.0-beta.1 (76b40532a 2019-11-05)

@hellow554
Copy link
Contributor

hellow554 commented Nov 11, 2019

nearly arbitrary custom syntax for example something requiring to wrap a single element into a parenthesis.

My head can't come up with an example that requires parentheses. macro_rules! macros are hygenic, so there should be no problem in omitting the parentheses and just writing primary_key = id will be fine.

Can you give me a counter example?

@weiznich
Copy link
Contributor Author

@hellow554 A real world example is the diesel table! macro. Somewhere in the inner it does some clever things to support plain types and tuples by just saying ($($some_ty),+) where $some_ty is some repeated type variable. Depending on the number of actual types there this will result in ($some_ty) which is a single type or ($some_ty1, $some_ty2, …) which is a tuple. It's probably possible to rewrite that code doing the same stuff but that would involve a lot of duplication (Because than you would need to have a special case for a single type everywhere).

@hellow554
Copy link
Contributor

hellow554 commented Nov 12, 2019

So somehing like this:

macro_rules! foo {
    ($($foo:expr),+) => {
        ($($foo),*)
    }
}

fn main() {
    let _a = foo!(3, 4);
    let _b = foo!(3);
}

You're right. The question is: should we remove the lint for all macros? Only for macros that contain repetitive expansions? (Is that even possible to detect?)

@hellow554

This comment has been minimized.

@rustbot rustbot 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. labels Nov 12, 2019
@weiznich
Copy link
Contributor Author

Would be great to get that fixed till it is part of the next stable release.

@weiznich
Copy link
Contributor Author

This seems to occur first on nightly-2019-11-02

@Mark-Simulacrum Mark-Simulacrum added the regression-from-stable-to-beta Performance or correctness regression from stable to beta. label Nov 19, 2019
@pnkfelix
Copy link
Member

pnkfelix commented Nov 21, 2019

nominating for discussing at T-compiler + T-lang meeting, namely to take peoples' temperature with respect to the idea of just not linting this case when the flagged code appears as part of a macro definition.

@pnkfelix pnkfelix added I-nominated T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Nov 21, 2019
@pnkfelix
Copy link
Member

also, marking as P-high w.r.t. answer the question(s) posed here. (I personally don't think the severity of the actual impact is likely to be worth a P-high rating; the priority is likely just temporary until the discussion happens.)

@pnkfelix pnkfelix added the P-high High priority label Nov 21, 2019
@pnkfelix pnkfelix changed the title unused_parens triggerts on macro by example code. unused_parens triggers on macro by example code. Nov 21, 2019
@Centril Centril removed the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Nov 26, 2019
@Centril
Copy link
Contributor

Centril commented Nov 26, 2019

The language team discussed this last week. The general consensus was that we can stop linting when the redundant parens are inside the macro (as opposed to e.g. mac!( ( 1 + 2) ) where the spans are from the outside). We believe this is fine as the lint is mostly a style thing which does not protect from anything correctness or performance related.

@weiznich
Copy link
Contributor Author

I've played a bit around with this issue today. Seems like those conditions need to be adjusted to also check if we are in macro expanded code. I've tried something like value.span.from_expansion() but that does remove more warnings that we want.

@pnkfelix pnkfelix self-assigned this Nov 28, 2019
@pnkfelix
Copy link
Member

I think the questions have been basically answered in terms of what path we see best going forward. Self-assigning, removing I-nominated.

JohnTitor added a commit to JohnTitor/rust that referenced this issue Dec 12, 2019
…bank

Fix `unused_parens` triggers on macro by example code

Fix rust-lang#66295

Unfortunately this does also break [an existing test](https://github.com/rust-lang/rust/blob/4787e97475de6be9487e3d9255a9c2d3c0bf9252/src/test/ui/lint/issue-47775-nested-macro-unnecessary-parens-arg.rs#L22). I'm not sure how to handle that, because that seems to be quite similar to the allowed cases

If this gets accepted it would be great to backport this fix to beta.
@bors bors closed this as completed in 3aa9902 Dec 12, 2019
Mark-Simulacrum pushed a commit to Mark-Simulacrum/rust that referenced this issue Dec 14, 2019
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. P-high High priority regression-from-stable-to-beta Performance or correctness regression from stable to beta. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants