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

Skip unused_parens report for Paren(Path(..)) in macro. #123314

Merged
merged 1 commit into from
Apr 11, 2024

Conversation

surechen
Copy link
Contributor

@surechen surechen commented Apr 1, 2024

fixes #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

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?

@rustbot
Copy link
Collaborator

rustbot commented Apr 1, 2024

r? @Nadrieril

rustbot has assigned @Nadrieril.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 1, 2024
@Nadrieril
Copy link
Member

Nadrieril commented Apr 1, 2024

It would be better to fix the incorrect span detection. I don't know how easy that is however

@cjgillot
Copy link
Contributor

cjgillot commented Apr 7, 2024

The lint is correct to fire here, only the suggestion is wrong. Looking at this kind of code, the compiler should assume the author meant <(T3,)>::bar() (notice the comma).

@surechen
Copy link
Contributor Author

surechen commented Apr 8, 2024

The lint is correct to fire here, only the suggestion is wrong. Looking at this kind of code, the compiler should assume the author meant <(T3,)>::bar() (notice the comma).

Thank you. I agree with you. I initially wanted to do like this.
Since this lint detects the ast expr after macro expansion, In fact, I don't how to find the macro by a ast expr, and how to judge the form of the macro. I will try to do it again. If you have some idea, please give me some help.

@cjgillot
Copy link
Contributor

cjgillot commented Apr 8, 2024

In the call to emit_unused_delims, the spans argument tells where are the parentheses, and to use them for a suggestion. If spans is None, there is no suggestion.
Pedantically, we should strive to suggest <$($rest),*>::bar(), ie just remove the parentheses. Maybe using start_point/end_point instead of looking at the inner type?
From a brief look, all the calls to emit_unused_delims use a spans argument constructed the same way. So they may all suffer from the same bug. It's a good opportunity to exercise all cases in the test.

@surechen
Copy link
Contributor Author

surechen commented Apr 9, 2024

In the call to emit_unused_delims, the spans argument tells where are the parentheses, and to use them for a suggestion. If spans is None, there is no suggestion. Pedantically, we should strive to suggest <$($rest),*>::bar(), ie just remove the parentheses. Maybe using start_point/end_point instead of looking at the inner type? From a brief look, all the calls to emit_unused_delims use a spans argument constructed the same way. So they may all suffer from the same bug. It's a good opportunity to exercise all cases in the test.

Thank you very much, I will read and think about your suggestions as soon as possible

@rust-log-analyzer

This comment has been minimized.

@surechen
Copy link
Contributor Author

surechen commented Apr 10, 2024

It would be better to fix the incorrect span detection. I don't know how easy that is however

Thank you very much.

I find there are already some measures to avoid reporting errors for conditions inside macros, such as https://github.com/rust-lang/rust/blob/master/compiler/rustc_lint/src/unused.rs#L1023 and https://github.com/rust-lang/rust/blob/master/compiler/rustc_lint/src/unused.rs#L1353.

Because it's hard to give correct suggestion for <($($rest),*)>::bar() to <($($rest,)*)>::bar().
Other suggestion like <$rest>::bar() or even <$($rest),*>::bar() will also cause compiler error.
So I just give error report and avoid suggestion for in macro expand.

Please see if it meets expectations?

@Nadrieril
Copy link
Member

Yep, that's a fine solution. Thank you!

@bors r+

@bors
Copy link
Contributor

bors commented Apr 10, 2024

📌 Commit c8490a0 has been approved by Nadrieril

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 10, 2024
fmease added a commit to fmease/rust that referenced this pull request 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?
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 10, 2024
Rollup of 6 pull requests

Successful merges:

 - rust-lang#122470 (`f16` and `f128` step 4: basic library support)
 - rust-lang#122954 (Be more specific when flagging imports as redundant due to the extern prelude)
 - rust-lang#123314 (Skip `unused_parens` report for `Paren(Path(..))` in macro.)
 - rust-lang#123360 (Document restricted_std)
 - rust-lang#123703 (Use `fn` ptr signature instead of `{closure@..}` in infer error)
 - rust-lang#123732 (Factor some common `io::Error` constants)

r? `@ghost`
`@rustbot` modify labels: rollup
fmease added a commit to fmease/rust that referenced this pull request 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?
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 10, 2024
Rollup of 7 pull requests

Successful merges:

 - rust-lang#122470 (`f16` and `f128` step 4: basic library support)
 - rust-lang#122954 (Be more specific when flagging imports as redundant due to the extern prelude)
 - rust-lang#123314 (Skip `unused_parens` report for `Paren(Path(..))` in macro.)
 - rust-lang#123360 (Document restricted_std)
 - rust-lang#123661 (Stabilize `cstr_count_bytes`)
 - rust-lang#123703 (Use `fn` ptr signature instead of `{closure@..}` in infer error)
 - rust-lang#123704 (Tweak value suggestions in `borrowck` and `hir_analysis`)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 11, 2024
Rollup of 8 pull requests

Successful merges:

 - rust-lang#122470 (`f16` and `f128` step 4: basic library support)
 - rust-lang#122954 (Be more specific when flagging imports as redundant due to the extern prelude)
 - rust-lang#123314 (Skip `unused_parens` report for `Paren(Path(..))` in macro.)
 - rust-lang#123360 (Document restricted_std)
 - rust-lang#123661 (Stabilize `cstr_count_bytes`)
 - rust-lang#123703 (Use `fn` ptr signature instead of `{closure@..}` in infer error)
 - rust-lang#123756 (clean up docs for `File::sync_*`)
 - rust-lang#123761 (Use `suggest_impl_trait` in return type suggestion on type error)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 11, 2024
Rollup of 8 pull requests

Successful merges:

 - rust-lang#122470 (`f16` and `f128` step 4: basic library support)
 - rust-lang#122954 (Be more specific when flagging imports as redundant due to the extern prelude)
 - rust-lang#123314 (Skip `unused_parens` report for `Paren(Path(..))` in macro.)
 - rust-lang#123360 (Document restricted_std)
 - rust-lang#123661 (Stabilize `cstr_count_bytes`)
 - rust-lang#123703 (Use `fn` ptr signature instead of `{closure@..}` in infer error)
 - rust-lang#123756 (clean up docs for `File::sync_*`)
 - rust-lang#123761 (Use `suggest_impl_trait` in return type suggestion on type error)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 82c6f18 into rust-lang:master Apr 11, 2024
11 checks passed
@rustbot rustbot added this to the 1.79.0 milestone Apr 11, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request 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
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rustc says necessary parentheses are unnecessary
6 participants