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_qualifications shouldn't trigger on expanded code #96698

Closed
zecakeh opened this issue May 4, 2022 · 0 comments · Fixed by #121528
Closed

unused_qualifications shouldn't trigger on expanded code #96698

zecakeh opened this issue May 4, 2022 · 0 comments · Fixed by #121528
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@zecakeh
Copy link

zecakeh commented May 4, 2022

Given the following code:

use my_macros::my_enum_macro;
use super::a::AType;

my_enum_macro! {
  "key.a_type" => super::a,
  "key.b_type" => super::c::b,
  // Other variants
}

pub fn my_fn(param: AType) {
  // ...
}

Where my_enum_macro, among other things, uses the path provided for the variant to qualify a type based on the key. It so happens that for the first variant here, the type in the expanded code resolves to super::a::AType.

The current output is:

warning: unnecessary qualification
  --> src/my_mod.rs:5:3
   |
  5|         "key.a_type" => super::a,
   |         ^^^^^^^^^^^^^^^^^^^^^
   |
   = note: requested on the command line with `-W unused-qualifications`

Since the type is already imported at the top, the qualification is indeed unnecessary. But I would also expect to have the same behavior with my macro what other imports there are above.

That's why I believe that this warning shouldn't trigger on expanded code, or at least between regular and expanded code.

@zecakeh zecakeh added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 4, 2022
@bors bors closed this as completed in ed6d175 Mar 4, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 4, 2024
Rollup merge of rust-lang#121528 - Alexendoo:unused_qualifications, r=petrochenkov

Consider middle segments of paths in `unused_qualifications`

Currently `unused_qualifications` looks at the last segment of a path to see if it can be trimmed, this PR extends the check to the middle segments also

```rust
// currently linted
use std::env::args();
std::env::args(); // Removes `std::env::`
```
```rust
// newly linted
use std::env;
std::env::args(); // Removes `std::`
```

Paths with generics in them are now linted as long as the part being trimmed is before any generic args, e.g. it will now suggest trimming `std::vec::` from `std::vec::Vec<usize>`

Paths with any segments that are from an expansion are no longer linted

Fixes rust-lang#100979
Fixes rust-lang#96698
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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.

1 participant