Skip to content

Fix the primary span of redundant_pub_crate when flagging nameless items #14516

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

Merged
merged 1 commit into from
Apr 1, 2025

Conversation

fmease
Copy link
Member

@fmease fmease commented Apr 1, 2025

Found this while reviewing PR rust-lang/rust#138384: See rust-lang/rust#138384 (comment) in which I suggested a FIXME to be added that I'm now fixing in this PR.


Before this PR, redundant_pub_crate computed a broken primary Span when flagging items (hir::Items) that never bear a name (ForeignMod, GlobalAsm, Impl, Use(UseKind::Glob), Use(UseKind::ListStem)). Namely, it created a span whose high byte index is DUMMY_SP.hi() which is quite broken (DUMMY_SP is synonymous with 0..0 wrt. the entire SourceMap meaning it points at the start of the very first source file in the SourceMap).

Why did this happen? Before PR rust-lang/rust#138384, the offending line looked like let span = item.span.with_hi(item.ident.span.hi());. For nameless items, item.ident used to be set to Ident(sym::empty, DUMMY_SP). This is where the DUMMY_SP came from.

The code means to compute a "shorter item span" that doesn't include the "body" of items, only the "head" (similar to TyCtxt::def_span).

Examples of Clippy's butchered output on master
#![deny(clippy::redundant_pub_crate)]

mod m5 {
    pub mod m5_1 {}
    pub(crate) use m5_1::*;
}
error: pub(crate) import inside private module
 --> file.rs:1:1
  |
1 | / #![deny(clippy::redundant_pub_crate)]
2 | |
3 | | mod m5 {
4 | |     pub mod m5_1 {}
5 | |     pub(crate) use m5_1::*;
  | |    ^---------- help: consider using: `pub`
  | |____|
  |

Or if the SourceMap contains multiple files (notice how it leaks clippy.toml!):

error: pub(crate) import inside private module
 --> /home/fmease/programming/rust/clippy/clippy.toml:1:1
  |
1 | / avoid-breaking-exported-api = false
2 | |
3 | | check-inconsistent-struct-field-initializers = true
... |
6 | |
  | |_^
  |
 ::: file.rs:6:5
  |
6 |       pub(crate) use m5_1::{*}; // Glob
  |       ---------- help: consider using: `pub`
  |

Note: Currently, the only nameless item kind that can also have a visibility is Use(UseKind::{Glob,ListStem}). Thus I'm just falling back to the entire item's Span which wouldn't be that verbose. However, in the future Rust will feature impl restrictions (like pub(crate) impl Trait for Type {}, see RFC 3323 and rust-lang/rust#106074). Using item.span for these would be quite bad (it would include all associated items). Should I add a FIXME?


changelog: [redundant_pub_crate]: Fix the code highlighting for nameless items.

@rustbot
Copy link
Collaborator

rustbot commented Apr 1, 2025

r? @Manishearth

rustbot has assigned @Manishearth.
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 Apr 1, 2025
Copy link
Member Author

Choose a reason for hiding this comment

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

Output on master for comparison
warning: pub(crate) import inside private module
 --> file.rs:1:1
  |
1 | / #![warn(clippy::redundant_pub_crate)]
2 | |
3 | | mod m5 {
4 | |     pub mod m5_1 {}
5 | |     // Test that the primary span isn't butchered for item kinds that don't have an ident.
6 | |     pub(crate) use m5_1::*; //~ redundant_pub_crate
  | |    ^---------- help: consider using: `pub`
  | |____|
  |
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pub_crate
note: the lint level is defined here
 --> file.rs:1:9
  |
1 | #![warn(clippy::redundant_pub_crate)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: pub(crate) import inside private module
 --> file.rs:1:1
  |
1 | / #![warn(clippy::redundant_pub_crate)]
2 | |
3 | | mod m5 {
4 | |     pub mod m5_1 {}
... |
7 | |     #[rustfmt::skip]
8 | |     pub(crate) use m5_1::{*}; //~ redundant_pub_crate
  | |_____----------___________^
  |       |
  |       help: consider using: `pub`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pub_crate

@Manishearth
Copy link
Member

Good catch! lintcheck fails but it doesn't seem to be your fault

(unclear if the build queue cares about lintcheck)

@Manishearth Manishearth added this pull request to the merge queue Apr 1, 2025
Merged via the queue into rust-lang:master with commit 6ca6b09 Apr 1, 2025
10 of 11 checks passed
@fmease fmease deleted the red-pub-crate-fix-prim-sp branch April 1, 2025 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

3 participants