diff options
| author | Manish Goregaokar <manishsmail@gmail.com> | 2025-04-01 17:24:58 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-01 17:24:58 +0000 |
| commit | 6ca6b09646ad2164684d930944617c0901829c79 (patch) | |
| tree | 4fc1d27b39fa8a3a96c88fffe0b892c479591e51 /compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp | |
| parent | 227e43a860338a75d78834ead5cddf8eb3cf0062 (diff) | |
| parent | 54994b2d4b89a8af8c5a04799e7fa224c7103ea6 (diff) | |
| download | rust-6ca6b09646ad2164684d930944617c0901829c79.tar.gz rust-6ca6b09646ad2164684d930944617c0901829c79.zip | |
Fix the primary span of `redundant_pub_crate` when flagging nameless items (#14516)
Found this while reviewing PR
https://github.com/rust-lang/rust/pull/138384: See
https://github.com/rust-lang/rust/pull/138384#discussion_r1996111087 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::Item`s) 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
https://github.com/rust-lang/rust/pull/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`).
<details><summary>Examples of Clippy's butchered output on
master</summary>
```rs
#![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`
|
```
</details>
---
**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](https://rust-lang.github.io/rfcs/3323-restrictions.html) and
https://github.com/rust-lang/rust/pull/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.
Diffstat (limited to 'compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp')
0 files changed, 0 insertions, 0 deletions
