forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#85324 - FabianWolff:issue-85255, r=varkor
Warn about unused `pub` fields in non-`pub` structs This pull request fixes rust-lang#85255. The current implementation of dead code analysis is too prudent because it marks all `pub` fields of structs as live, even though they cannot be accessed from outside of the current crate if the struct itself only has restricted or private visibility. I have changed this behavior to take the containing struct's visibility into account when looking at field visibility and liveness. This also makes dead code warnings more consistent; consider the example given in rust-lang#85255: ```rust struct Foo { a: i32, pub b: i32, } struct Bar; impl Bar { fn a(&self) -> i32 { 5 } pub fn b(&self) -> i32 { 6 } } fn main() { let _ = Foo { a: 1, b: 2 }; let _ = Bar; } ``` Current nightly already warns about `Bar::b()`, even though it is `pub` (but `Bar` is not). It should therefore also warn about `Foo::b`, which it does with the changes in this PR.
- Loading branch information
Showing
6 changed files
with
87 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Unused `pub` fields in non-`pub` structs should also trigger dead code warnings. | ||
// check-pass | ||
|
||
#![warn(dead_code)] | ||
|
||
struct Foo { | ||
a: i32, //~ WARNING: field is never read | ||
pub b: i32, //~ WARNING: field is never read | ||
} | ||
|
||
struct Bar; | ||
|
||
impl Bar { | ||
fn a(&self) -> i32 { 5 } //~ WARNING: associated function is never used | ||
pub fn b(&self) -> i32 { 6 } //~ WARNING: associated function is never used | ||
} | ||
|
||
|
||
fn main() { | ||
let _ = Foo { a: 1, b: 2 }; | ||
let _ = Bar; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
warning: field is never read: `a` | ||
--> $DIR/issue-85255.rs:7:5 | ||
| | ||
LL | a: i32, | ||
| ^^^^^^ | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/issue-85255.rs:4:9 | ||
| | ||
LL | #![warn(dead_code)] | ||
| ^^^^^^^^^ | ||
|
||
warning: field is never read: `b` | ||
--> $DIR/issue-85255.rs:8:5 | ||
| | ||
LL | pub b: i32, | ||
| ^^^^^^^^^^ | ||
|
||
warning: associated function is never used: `a` | ||
--> $DIR/issue-85255.rs:14:8 | ||
| | ||
LL | fn a(&self) -> i32 { 5 } | ||
| ^ | ||
|
||
warning: associated function is never used: `b` | ||
--> $DIR/issue-85255.rs:15:12 | ||
| | ||
LL | pub fn b(&self) -> i32 { 6 } | ||
| ^ | ||
|
||
warning: 4 warnings emitted | ||
|