-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feature: add new lint pub_underscore_fields
#10283
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,83 @@ | ||
use clippy_config::types::PubUnderscoreFieldsBehaviour; | ||
use clippy_utils::attrs::is_doc_hidden; | ||
use clippy_utils::diagnostics::span_lint_and_help; | ||
use clippy_utils::is_path_lang_item; | ||
use rustc_hir::{FieldDef, Item, ItemKind, LangItem}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::impl_lint_pass; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks whether any field of the struct is prefixed with an `_` (underscore) and also marked | ||
/// `pub` (public) | ||
/// | ||
/// ### Why is this bad? | ||
/// Fields prefixed with an `_` are inferred as unused, which suggests it should not be marked | ||
/// as `pub`, because marking it as `pub` infers it will be used. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// struct FileHandle { | ||
/// pub _descriptor: usize, | ||
/// } | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// struct FileHandle { | ||
/// _descriptor: usize, | ||
/// } | ||
/// ``` | ||
/// | ||
/// OR | ||
/// | ||
/// ```rust | ||
/// struct FileHandle { | ||
/// pub descriptor: usize, | ||
/// } | ||
/// ``` | ||
#[clippy::version = "1.77.0"] | ||
pub PUB_UNDERSCORE_FIELDS, | ||
pedantic, | ||
"struct field prefixed with underscore and marked public" | ||
} | ||
|
||
pub struct PubUnderscoreFields { | ||
pub behavior: PubUnderscoreFieldsBehaviour, | ||
} | ||
impl_lint_pass!(PubUnderscoreFields => [PUB_UNDERSCORE_FIELDS]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for PubUnderscoreFields { | ||
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) { | ||
// This lint only pertains to structs. | ||
let ItemKind::Struct(variant_data, _) = &item.kind else { | ||
return; | ||
}; | ||
|
||
let is_visible = |field: &FieldDef<'_>| match self.behavior { | ||
PubUnderscoreFieldsBehaviour::PublicallyExported => cx.effective_visibilities.is_reachable(field.def_id), | ||
PubUnderscoreFieldsBehaviour::AllPubFields => { | ||
// If there is a visibility span then the field is marked pub in some way. | ||
!field.vis_span.is_empty() | ||
}, | ||
}; | ||
|
||
for field in variant_data.fields() { | ||
// Only pertains to fields that start with an underscore, and are public. | ||
if field.ident.as_str().starts_with('_') && is_visible(field) | ||
// We ignore fields that have `#[doc(hidden)]`. | ||
&& !is_doc_hidden(cx.tcx.hir().attrs(field.hir_id)) | ||
// We ignore fields that are `PhantomData`. | ||
&& !is_path_lang_item(cx, field.ty, LangItem::PhantomData) | ||
{ | ||
span_lint_and_help( | ||
cx, | ||
PUB_UNDERSCORE_FIELDS, | ||
field.vis_span.to(field.ident.span), | ||
"field marked as public but also inferred as unused because it's prefixed with `_`", | ||
None, | ||
"consider removing the underscore, or making the field private", | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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 @@ | ||
pub-underscore-fields-behavior = "AllPubFields" |
This file contains hidden or 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 @@ | ||
pub-underscore-fields-behavior = "PublicallyExported" |
60 changes: 60 additions & 0 deletions
60
tests/ui-toml/pub_underscore_fields/pub_underscore_fields.all_pub_fields.stderr
This file contains hidden or 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,60 @@ | ||
error: field marked as public but also inferred as unused because it's prefixed with `_` | ||
--> $DIR/pub_underscore_fields.rs:15:9 | ||
| | ||
LL | pub _b: u8, | ||
| ^^^^^^ | ||
| | ||
= help: consider removing the underscore, or making the field private | ||
= note: `-D clippy::pub-underscore-fields` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::pub_underscore_fields)]` | ||
|
||
error: field marked as public but also inferred as unused because it's prefixed with `_` | ||
--> $DIR/pub_underscore_fields.rs:23:13 | ||
| | ||
LL | pub(in crate::inner) _f: Option<()>, | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider removing the underscore, or making the field private | ||
|
||
error: field marked as public but also inferred as unused because it's prefixed with `_` | ||
--> $DIR/pub_underscore_fields.rs:27:13 | ||
| | ||
LL | pub _g: String, | ||
| ^^^^^^ | ||
| | ||
= help: consider removing the underscore, or making the field private | ||
|
||
error: field marked as public but also inferred as unused because it's prefixed with `_` | ||
--> $DIR/pub_underscore_fields.rs:34:9 | ||
| | ||
LL | pub _a: usize, | ||
| ^^^^^^ | ||
| | ||
= help: consider removing the underscore, or making the field private | ||
|
||
error: field marked as public but also inferred as unused because it's prefixed with `_` | ||
--> $DIR/pub_underscore_fields.rs:41:9 | ||
| | ||
LL | pub _c: i64, | ||
| ^^^^^^ | ||
| | ||
= help: consider removing the underscore, or making the field private | ||
|
||
error: field marked as public but also inferred as unused because it's prefixed with `_` | ||
--> $DIR/pub_underscore_fields.rs:44:9 | ||
| | ||
LL | pub _e: Option<u8>, | ||
| ^^^^^^ | ||
| | ||
= help: consider removing the underscore, or making the field private | ||
|
||
error: field marked as public but also inferred as unused because it's prefixed with `_` | ||
--> $DIR/pub_underscore_fields.rs:57:9 | ||
| | ||
LL | pub(crate) _b: Option<String>, | ||
| ^^^^^^^^^^^^^ | ||
| | ||
= help: consider removing the underscore, or making the field private | ||
|
||
error: aborting due to 7 previous errors | ||
|
12 changes: 12 additions & 0 deletions
12
tests/ui-toml/pub_underscore_fields/pub_underscore_fields.exported.stderr
This file contains hidden or 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,12 @@ | ||
error: field marked as public but also inferred as unused because it's prefixed with `_` | ||
--> $DIR/pub_underscore_fields.rs:15:9 | ||
| | ||
LL | pub _b: u8, | ||
| ^^^^^^ | ||
| | ||
= help: consider removing the underscore, or making the field private | ||
= note: `-D clippy::pub-underscore-fields` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::pub_underscore_fields)]` | ||
|
||
error: aborting due to 1 previous error | ||
|
66 changes: 66 additions & 0 deletions
66
tests/ui-toml/pub_underscore_fields/pub_underscore_fields.rs
This file contains hidden or 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,66 @@ | ||
//@revisions: exported all_pub_fields | ||
//@[all_pub_fields] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/pub_underscore_fields/all_pub_fields | ||
//@[exported] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/pub_underscore_fields/exported | ||
|
||
#![allow(unused)] | ||
#![warn(clippy::pub_underscore_fields)] | ||
|
||
use std::marker::PhantomData; | ||
|
||
pub mod inner { | ||
use std::marker; | ||
|
||
pub struct PubSuper { | ||
pub(super) a: usize, | ||
pub _b: u8, | ||
_c: i32, | ||
pub _mark: marker::PhantomData<u8>, | ||
} | ||
|
||
mod inner2 { | ||
pub struct PubModVisibility { | ||
pub(in crate::inner) e: bool, | ||
pub(in crate::inner) _f: Option<()>, | ||
} | ||
|
||
struct PrivateStructPubField { | ||
pub _g: String, | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
pub struct StructWithOneViolation { | ||
pub _a: usize, | ||
} | ||
|
||
// should handle structs with multiple violations | ||
pub struct StructWithMultipleViolations { | ||
a: u8, | ||
_b: usize, | ||
pub _c: i64, | ||
#[doc(hidden)] | ||
pub d: String, | ||
pub _e: Option<u8>, | ||
} | ||
|
||
// shouldn't warn on anonymous fields | ||
pub struct AnonymousFields(pub usize, i32); | ||
|
||
// don't warn on empty structs | ||
pub struct Empty1; | ||
pub struct Empty2(); | ||
pub struct Empty3 {}; | ||
|
||
pub struct PubCrate { | ||
pub(crate) a: String, | ||
pub(crate) _b: Option<String>, | ||
} | ||
|
||
// shouldn't warn on fields named pub | ||
pub struct NamedPub { | ||
r#pub: bool, | ||
_pub: String, | ||
pub(crate) _mark: PhantomData<u8>, | ||
} | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.