Skip to content

Commit 40d6387

Browse files
committed
Add new lint partial_pub_fields
Signed-off-by: TennyZhuang <zty0826@gmail.com>
1 parent ff33d6e commit 40d6387

File tree

9 files changed

+152
-0
lines changed

9 files changed

+152
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4134,6 +4134,7 @@ Released 2018-09-13
41344134
[`panic_in_result_fn`]: https://rust-lang.github.io/rust-clippy/master/index.html#panic_in_result_fn
41354135
[`panic_params`]: https://rust-lang.github.io/rust-clippy/master/index.html#panic_params
41364136
[`panicking_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#panicking_unwrap
4137+
[`partial_pub_fields`]: https://rust-lang.github.io/rust-clippy/master/index.html#partial_pub_fields
41374138
[`partialeq_ne_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#partialeq_ne_impl
41384139
[`partialeq_to_none`]: https://rust-lang.github.io/rust-clippy/master/index.html#partialeq_to_none
41394140
[`path_buf_push_overwrite`]: https://rust-lang.github.io/rust-clippy/master/index.html#path_buf_push_overwrite

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ store.register_lints(&[
479479
panic_unimplemented::TODO,
480480
panic_unimplemented::UNIMPLEMENTED,
481481
panic_unimplemented::UNREACHABLE,
482+
partial_pub_fields::PARTIAL_PUB_FIELDS,
482483
partialeq_ne_impl::PARTIALEQ_NE_IMPL,
483484
partialeq_to_none::PARTIALEQ_TO_NONE,
484485
pass_by_ref_or_value::LARGE_TYPES_PASSED_BY_VALUE,

clippy_lints/src/lib.register_nursery.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ store.register_group(true, "clippy::nursery", Some("clippy_nursery"), vec![
2727
LintId::of(non_send_fields_in_send_ty::NON_SEND_FIELDS_IN_SEND_TY),
2828
LintId::of(nonstandard_macro_braces::NONSTANDARD_MACRO_BRACES),
2929
LintId::of(option_if_let_else::OPTION_IF_LET_ELSE),
30+
LintId::of(partial_pub_fields::PARTIAL_PUB_FIELDS),
3031
LintId::of(redundant_pub_crate::REDUNDANT_PUB_CRATE),
3132
LintId::of(regex::TRIVIAL_REGEX),
3233
LintId::of(strings::STRING_LIT_AS_BYTES),

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ mod option_if_let_else;
324324
mod overflow_check_conditional;
325325
mod panic_in_result_fn;
326326
mod panic_unimplemented;
327+
mod partial_pub_fields;
327328
mod partialeq_ne_impl;
328329
mod partialeq_to_none;
329330
mod pass_by_ref_or_value;
@@ -908,6 +909,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
908909
store.register_late_pass(|_| Box::new(bool_to_int_with_if::BoolToIntWithIf));
909910
store.register_late_pass(|_| Box::new(box_default::BoxDefault));
910911
store.register_late_pass(|_| Box::new(implicit_saturating_add::ImplicitSaturatingAdd));
912+
store.register_early_pass(|| Box::new(partial_pub_fields::PartialPubFields));
911913
// add lints here, do not remove this comment, it's used in `new_lint`
912914
}
913915

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
use clippy_utils::diagnostics::span_lint_and_help;
2+
use rustc_ast::ast::*;
3+
use rustc_lint::{EarlyContext, EarlyLintPass};
4+
use rustc_session::{declare_lint_pass, declare_tool_lint};
5+
6+
declare_clippy_lint! {
7+
/// ### What it does
8+
/// Checks whether partial fields of a struct are public.
9+
///
10+
/// Either make all fields of a type public, or make none of them public
11+
///
12+
/// ### Why is this bad?
13+
/// Most types should either be:
14+
/// * Abstract data types: complex objects with opaque implementation which guard
15+
/// interior invariants and expose intentionally limited API to the outside world.
16+
/// * Data: relatively simple objects which group a bunch of related attributes together.
17+
///
18+
/// ### Example
19+
/// ```rust
20+
/// pub struct Color {
21+
/// pub r,
22+
/// pub g,
23+
/// b,
24+
/// }
25+
/// ```
26+
/// Use instead:
27+
/// ```rust
28+
/// pub struct Color {
29+
/// pub r,
30+
/// pub g,
31+
/// pub b,
32+
/// }
33+
/// ```
34+
#[clippy::version = "1.66.0"]
35+
pub PARTIAL_PUB_FIELDS,
36+
restriction,
37+
"partial fields of a struct are public"
38+
}
39+
declare_lint_pass!(PartialPubFields => [PARTIAL_PUB_FIELDS]);
40+
41+
impl EarlyLintPass for PartialPubFields {
42+
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
43+
let ItemKind::Struct(ref st, _) = item.kind else {
44+
return;
45+
};
46+
47+
let mut fields = st.fields().iter();
48+
let Some(first_field) = fields.next() else {
49+
// Empty struct.
50+
return;
51+
};
52+
let all_pub = first_field.vis.kind.is_pub();
53+
let all_priv = !all_pub;
54+
55+
let msg = "mixed usage of pub and non-pub fields";
56+
57+
for field in fields {
58+
if all_priv && field.vis.kind.is_pub() {
59+
span_lint_and_help(
60+
cx,
61+
&PARTIAL_PUB_FIELDS,
62+
field.vis.span,
63+
msg,
64+
None,
65+
"consider using private field here",
66+
);
67+
return;
68+
} else if all_pub && !field.vis.kind.is_pub() {
69+
span_lint_and_help(
70+
cx,
71+
&PARTIAL_PUB_FIELDS,
72+
field.vis.span,
73+
msg,
74+
None,
75+
"consider using public field here",
76+
);
77+
return;
78+
}
79+
}
80+
}
81+
}

src/docs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ docs! {
395395
"panic",
396396
"panic_in_result_fn",
397397
"panicking_unwrap",
398+
"partial_pub_fields",
398399
"partialeq_ne_impl",
399400
"partialeq_to_none",
400401
"path_buf_push_overwrite",

src/docs/partial_pub_fields.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
### What it does
2+
Checks whether partial fields of a struct are public.
3+
4+
Either make all fields of a type public, or make none of them public
5+
6+
### Why is this bad?
7+
Most types should either be:
8+
* Abstract data types: complex objects with opaque implementation which guard interior invariants and expose intentionally limited API to the outside world.
9+
* Data: relatively simple objects which group a bunch of related attributes together.
10+
11+
### Example
12+
```rust
13+
pub struct Color {
14+
pub r,
15+
pub g,
16+
b,
17+
}
18+
```
19+
Use instead:
20+
```rust
21+
pub struct Color {
22+
pub r,
23+
pub g,
24+
pub b,
25+
}
26+
```

tests/ui/partial_pub_fields.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![allow(unused)]
2+
#![warn(clippy::partial_pub_fields)]
3+
4+
fn main() {
5+
// test code goes here
6+
7+
use std::collections::HashMap;
8+
9+
#[derive(Default)]
10+
pub struct FileSet {
11+
files: HashMap<String, u32>,
12+
pub paths: HashMap<u32, String>,
13+
}
14+
15+
pub struct Color {
16+
pub r: u8,
17+
pub g: u8,
18+
b: u8,
19+
}
20+
}

tests/ui/partial_pub_fields.stderr

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: mixed usage of pub and non-pub fields
2+
--> $DIR/partial_pub_fields.rs:12:9
3+
|
4+
LL | pub paths: HashMap<u32, String>,
5+
| ^^^
6+
|
7+
= help: consider using private field here
8+
= note: `-D clippy::partial-pub-fields` implied by `-D warnings`
9+
10+
error: mixed usage of pub and non-pub fields
11+
--> $DIR/partial_pub_fields.rs:18:9
12+
|
13+
LL | b: u8,
14+
| ^
15+
|
16+
= help: consider using public field here
17+
18+
error: aborting due to 2 previous errors
19+

0 commit comments

Comments
 (0)