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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions clippy_lints/src/redundant_pub_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,10 @@ impl<'tcx> LateLintPass<'tcx> for RedundantPubCrate {
&& is_not_macro_export(item)
&& !item.span.in_external_macro(cx.sess().source_map())
{
// FIXME: `DUMMY_SP` isn't right here, because it causes the
// resulting span to begin at the start of the file.
let span = item.span.with_hi(
item.kind
.ident()
.map_or(rustc_span::DUMMY_SP.hi(), |ident| ident.span.hi()),
);
let span = item
.kind
.ident()
.map_or(item.span, |ident| item.span.with_hi(ident.span.hi()));
let descr = cx.tcx.def_kind(item.owner_id).descr(item.owner_id.to_def_id());
span_lint_and_then(
cx,
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/redundant_pub_crate.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ mod m4 {
}
}

mod m5 {
pub mod m5_1 {}
// Test that the primary span isn't butchered for item kinds that don't have an ident.
pub use m5_1::*; //~ redundant_pub_crate
#[rustfmt::skip]
pub use m5_1::{*}; //~ redundant_pub_crate
}

pub use m4::*;

mod issue_8732 {
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/redundant_pub_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ mod m4 {
}
}

mod m5 {
pub mod m5_1 {}
// Test that the primary span isn't butchered for item kinds that don't have an ident.
pub(crate) use m5_1::*; //~ redundant_pub_crate
#[rustfmt::skip]
pub(crate) use m5_1::{*}; //~ redundant_pub_crate
}

pub use m4::*;

mod issue_8732 {
Expand Down
18 changes: 17 additions & 1 deletion tests/ui/redundant_pub_crate.stderr
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

Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,21 @@ LL | pub(crate) fn g() {} // private due to m4_2
| |
| help: consider using: `pub`

error: aborting due to 16 previous errors
error: pub(crate) import inside private module
--> tests/ui/redundant_pub_crate.rs:137:5
|
LL | pub(crate) use m5_1::*;
| ----------^^^^^^^^^^^^^
| |
| help: consider using: `pub`

error: pub(crate) import inside private module
--> tests/ui/redundant_pub_crate.rs:139:27
|
LL | pub(crate) use m5_1::{*};
| ---------- ^
| |
| help: consider using: `pub`

error: aborting due to 18 previous errors

Loading