-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add new lint partial_pub_fields
#9658
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use clippy_utils::diagnostics::span_lint_and_help; | ||
use rustc_ast::ast::{Item, ItemKind}; | ||
use rustc_lint::{EarlyContext, EarlyLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks whether partial fields of a struct are public. | ||
/// | ||
/// Either make all fields of a type public, or make none of them public | ||
/// | ||
/// ### Why is this bad? | ||
/// Most types should either be: | ||
/// * Abstract data types: complex objects with opaque implementation which guard | ||
/// interior invariants and expose intentionally limited API to the outside world. | ||
/// * Data: relatively simple objects which group a bunch of related attributes together. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// pub struct Color { | ||
/// pub r: u8, | ||
/// pub g: u8, | ||
/// b: u8, | ||
/// } | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// pub struct Color { | ||
/// pub r: u8, | ||
/// pub g: u8, | ||
/// pub b: u8, | ||
/// } | ||
/// ``` | ||
#[clippy::version = "1.66.0"] | ||
pub PARTIAL_PUB_FIELDS, | ||
restriction, | ||
"partial fields of a struct are public" | ||
} | ||
declare_lint_pass!(PartialPubFields => [PARTIAL_PUB_FIELDS]); | ||
|
||
impl EarlyLintPass for PartialPubFields { | ||
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { | ||
let ItemKind::Struct(ref st, _) = item.kind else { | ||
return; | ||
}; | ||
|
||
let mut fields = st.fields().iter(); | ||
let Some(first_field) = fields.next() else { | ||
// Empty struct. | ||
return; | ||
}; | ||
let all_pub = first_field.vis.kind.is_pub(); | ||
let all_priv = !all_pub; | ||
|
||
let msg = "mixed usage of pub and non-pub fields"; | ||
|
||
for field in fields { | ||
if all_priv && field.vis.kind.is_pub() { | ||
span_lint_and_help( | ||
cx, | ||
PARTIAL_PUB_FIELDS, | ||
field.vis.span, | ||
msg, | ||
None, | ||
"consider using private field here", | ||
); | ||
return; | ||
} else if all_pub && !field.vis.kind.is_pub() { | ||
span_lint_and_help( | ||
cx, | ||
PARTIAL_PUB_FIELDS, | ||
field.vis.span, | ||
msg, | ||
None, | ||
"consider using public field here", | ||
); | ||
return; | ||
} | ||
} | ||
} | ||
} |
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,27 @@ | ||
### What it does | ||
Checks whether partial fields of a struct are public. | ||
|
||
Either make all fields of a type public, or make none of them public | ||
|
||
### Why is this bad? | ||
Most types should either be: | ||
* Abstract data types: complex objects with opaque implementation which guard | ||
interior invariants and expose intentionally limited API to the outside world. | ||
* Data: relatively simple objects which group a bunch of related attributes together. | ||
|
||
### Example | ||
``` | ||
pub struct Color { | ||
pub r: u8, | ||
pub g: u8, | ||
b: u8, | ||
} | ||
``` | ||
Use instead: | ||
``` | ||
pub struct Color { | ||
pub r: u8, | ||
pub g: u8, | ||
pub b: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#![allow(unused)] | ||
#![warn(clippy::partial_pub_fields)] | ||
|
||
fn main() { | ||
use std::collections::HashMap; | ||
|
||
#[derive(Default)] | ||
pub struct FileSet { | ||
files: HashMap<String, u32>, | ||
pub paths: HashMap<u32, String>, | ||
} | ||
|
||
pub struct Color { | ||
pub r: u8, | ||
pub g: u8, | ||
b: u8, | ||
} | ||
|
||
pub struct Point(i32, pub i32); | ||
|
||
pub struct Visibility { | ||
r#pub: bool, | ||
pub pos: u32, | ||
} | ||
|
||
// Don't lint on empty structs; | ||
pub struct Empty1; | ||
pub struct Empty2(); | ||
pub struct Empty3 {}; | ||
|
||
// Don't lint on structs with one field. | ||
pub struct Single1(i32); | ||
pub struct Single2(pub i32); | ||
pub struct Single3 { | ||
v1: i32, | ||
} | ||
pub struct Single4 { | ||
pub v1: i32, | ||
} | ||
} | ||
TennyZhuang marked this conversation as resolved.
Show resolved
Hide resolved
|
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,35 @@ | ||
error: mixed usage of pub and non-pub fields | ||
--> $DIR/partial_pub_fields.rs:10:9 | ||
| | ||
LL | pub paths: HashMap<u32, String>, | ||
| ^^^ | ||
| | ||
= help: consider using private field here | ||
= note: `-D clippy::partial-pub-fields` implied by `-D warnings` | ||
|
||
error: mixed usage of pub and non-pub fields | ||
--> $DIR/partial_pub_fields.rs:16:9 | ||
| | ||
LL | b: u8, | ||
| ^ | ||
| | ||
= help: consider using public field here | ||
|
||
error: mixed usage of pub and non-pub fields | ||
--> $DIR/partial_pub_fields.rs:19:27 | ||
| | ||
LL | pub struct Point(i32, pub i32); | ||
| ^^^ | ||
| | ||
= help: consider using private field here | ||
|
||
error: mixed usage of pub and non-pub fields | ||
--> $DIR/partial_pub_fields.rs:23:9 | ||
| | ||
LL | pub pos: u32, | ||
| ^^^ | ||
| | ||
= help: consider using private field here | ||
|
||
error: aborting due to 4 previous errors | ||
|
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.