Skip to content

new lint: use_crate_prefix_for_self_imports - #13662

Open
lengyijun wants to merge 25 commits into
rust-lang:masterfrom
lengyijun:last_use_crate_prefix_for_self_imports
Open

lengyijun wants to merge 25 commits into
rust-lang:masterfrom
lengyijun:last_use_crate_prefix_for_self_imports

Conversation

@lengyijun

@lengyijun lengyijun commented Nov 7, 2024 •

Copy link
Copy Markdown
Contributor

View all comments

changelog: [use_crate_prefix_for_self_imports]: new lint

fix #13645

Only check main.rs and lib.rs

Known problem

  • Duplicate warning

TODO

  • rm tests/ui/use_crate_prefix_for_self_imports.rs
  • squash commits

Question

Although pass all tests, but if copy lintcheck to /tmp, clippy will report two warnings (expected)

warning: this import is not clear
  --> src/main.rs:42:5
   |
42 | use input::read_crates;
   |     ^^^^^ help: prefix with `crate::`: `crate::input`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#use_crate_prefix_for_self_imports
   = note: `#[warn(clippy::use_crate_prefix_for_self_imports)]` on by default

warning: this import is not clear
  --> src/main.rs:43:5
   |
43 | use output::{ClippyCheckOutput, ClippyWarning, RustcIce};
   |     ^^^^^^ help: prefix with `crate::`: `crate::output`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#use_crate_prefix_for_self_imports

@rustbot

rustbot commented Nov 7, 2024

Copy link
Copy Markdown
Collaborator

r? @llogiq

rustbot has assigned @llogiq.
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 the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Nov 7, 2024
@lengyijun
lengyijun force-pushed the last_use_crate_prefix_for_self_imports branch from 2a70749 to bfc0849 Compare November 7, 2024 11:40
@lengyijun lengyijun changed the title use crate prefix for self imports new lint use_crate_prefix_for_self_imports Nov 7, 2024
@lengyijun lengyijun changed the title new lint use_crate_prefix_for_self_imports new lint: use_crate_prefix_for_self_imports Nov 7, 2024
@lengyijun
lengyijun force-pushed the last_use_crate_prefix_for_self_imports branch 5 times, most recently from f74a6bc to 6a0c669 Compare November 8, 2024 04:46
Comment thread clippy_lints/src/use_crate_prefix_for_self_imports.rs Outdated
@lengyijun
lengyijun force-pushed the last_use_crate_prefix_for_self_imports branch from 6a0c669 to 7633f16 Compare November 13, 2024 03:26
@llogiq

llogiq commented Jan 10, 2025

Copy link
Copy Markdown
Contributor

So, as you know we discussed this on zulip a while ago. The upshot is that we have two acceptable options:

  • Put the lint in the nursery group and improve it until we deem it ready for style
  • Add more checks to the lint to reduce the number of warnings that aren't too helpful. For example, if we have the respective mod near the use (where near could be measured in line number differences or in no other items but mods and uses in between), we could avoid linting, as the reader of the code will know that the item comes from the mod specified nearby (this could be done by walking the sibling items until a non-mod-or-use item is found).

Do you intend to continue working on this?

@llogiq

llogiq commented Jan 25, 2025

Copy link
Copy Markdown
Contributor

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jan 25, 2025
@lengyijun
lengyijun force-pushed the last_use_crate_prefix_for_self_imports branch from 7633f16 to 5817b6f Compare January 27, 2025 10:20
Comment thread tests/ui/use_crate_prefix_for_self_imports.rs Outdated
@lengyijun
lengyijun force-pushed the last_use_crate_prefix_for_self_imports branch 5 times, most recently from 840f04d to 9e1dbdc Compare January 30, 2025 04:22
@lengyijun

Copy link
Copy Markdown
Contributor Author

Don't lint if mod and use in the same block

@lengyijun
lengyijun force-pushed the last_use_crate_prefix_for_self_imports branch from a500545 to a3f5529 Compare January 31, 2025 01:01
@lengyijun

Copy link
Copy Markdown
Contributor Author

The problem of lintcheck still exists

@llogiq

llogiq commented Feb 2, 2025

Copy link
Copy Markdown
Contributor

What does that mean?

@lengyijun

Copy link
Copy Markdown
Contributor Author

Although pass all tests, but if copy lintcheck to /tmp, clippy will report two warnings (expected)

warning: this import is not clear
  --> src/main.rs:42:5
   |
42 | use input::read_crates;
   |     ^^^^^ help: prefix with `crate::`: `crate::input`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#use_crate_prefix_for_self_imports
   = note: `#[warn(clippy::use_crate_prefix_for_self_imports)]` on by default

warning: this import is not clear
  --> src/main.rs:43:5
   |
43 | use output::{ClippyCheckOutput, ClippyWarning, RustcIce};
   |     ^^^^^^ help: prefix with `crate::`: `crate::output`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#use_crate_prefix_for_self_imports

@lengyijun
lengyijun force-pushed the last_use_crate_prefix_for_self_imports branch from fcf45ca to 410d275 Compare September 3, 2026 11:25
@rustbot

rustbot commented Sep 3, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@lengyijun
lengyijun force-pushed the last_use_crate_prefix_for_self_imports branch 3 times, most recently from 4142dfa to 20a5fb6 Compare September 3, 2026 12:39
@lengyijun
lengyijun force-pushed the last_use_crate_prefix_for_self_imports branch from 20a5fb6 to 1554268 Compare September 3, 2026 12:44
@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown

Lintcheck changes for 1554268

Lint Added Removed Changed
clippy::use_crate_prefix_for_self_imports 128 0 0

This comment will be updated if you push new changes

@lengyijun

Copy link
Copy Markdown
Contributor Author

@llogiq @Jarcho #13662 (comment) The problem still exists

Comment on lines +69 to +83
let FileName::Real(p) = cx.sess().source_map().span_to_filename(item.span) else {
self.clear();
return;
};
let Some(p) = p.local_path() else {
return;
};
let Some(file_name) = p.file_name() else {
self.clear();
return;
};
// only check `main.rs` and `lib.rs`
if !(file_name == "main.rs" || file_name == "lib.rs") {
return;
}

@Jarcho Jarcho Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first symbol in the path is either a crate or a name in the current scope. For items in the current scope they're defined at either the module level or the statement level. All those at the module level can always use the self:: prefix.

View changes since the review

@Jarcho

Jarcho commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

@rustbot label lint-nominated

@rustbot rustbot added the lint-nominated Create an FCP-thread on Zulip for this PR label Sep 4, 2026
@rustbot

rustbot commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

This lint has been nominated for inclusion.

A FCP topic has been created on Zulip.

@CommanderStorm CommanderStorm mentioned this pull request Sep 6, 2026
6 tasks done
@rustbot

rustbot commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator

☔ The latest upstream changes (possibly #13598) made this pull request unmergeable. Please resolve the merge conflicts.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lint-nominated Create an FCP-thread on Zulip for this PR needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

use_crate_prefix_for_self_imports

5 participants