-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: TennyZhuang <[email protected]>
- Loading branch information
1 parent
ff33d6e
commit 40d6387
Showing
9 changed files
with
152 additions
and
0 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,81 @@ | ||
use clippy_utils::diagnostics::span_lint_and_help; | ||
use rustc_ast::ast::*; | ||
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, | ||
/// pub g, | ||
/// b, | ||
/// } | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// pub struct Color { | ||
/// pub r, | ||
/// pub g, | ||
/// pub b, | ||
/// } | ||
/// ``` | ||
#[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 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,26 @@ | ||
### 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, | ||
pub g, | ||
b, | ||
} | ||
``` | ||
Use instead: | ||
```rust | ||
pub struct Color { | ||
pub r, | ||
pub g, | ||
pub b, | ||
} | ||
``` |
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,20 @@ | ||
#![allow(unused)] | ||
#![warn(clippy::partial_pub_fields)] | ||
|
||
fn main() { | ||
// test code goes here | ||
|
||
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, | ||
} | ||
} |
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,19 @@ | ||
error: mixed usage of pub and non-pub fields | ||
--> $DIR/partial_pub_fields.rs:12: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:18:9 | ||
| | ||
LL | b: u8, | ||
| ^ | ||
| | ||
= help: consider using public field here | ||
|
||
error: aborting due to 2 previous errors | ||
|