Add new lint partial_pub_fields

Signed-off-by: TennyZhuang <zty0826@gmail.com>
This commit is contained in:
TennyZhuang 2022-10-16 16:02:23 +08:00
parent ff33d6e712
commit 7ac97b69fc
9 changed files with 153 additions and 0 deletions

View file

@ -479,6 +479,7 @@ store.register_lints(&[
panic_unimplemented::TODO,
panic_unimplemented::UNIMPLEMENTED,
panic_unimplemented::UNREACHABLE,
partial_pub_fields::PARTIAL_PUB_FIELDS,
partialeq_ne_impl::PARTIALEQ_NE_IMPL,
partialeq_to_none::PARTIALEQ_TO_NONE,
pass_by_ref_or_value::LARGE_TYPES_PASSED_BY_VALUE,

View file

@ -61,6 +61,7 @@ store.register_group(true, "clippy::restriction", Some("clippy_restriction"), ve
LintId::of(panic_unimplemented::TODO),
LintId::of(panic_unimplemented::UNIMPLEMENTED),
LintId::of(panic_unimplemented::UNREACHABLE),
LintId::of(partial_pub_fields::PARTIAL_PUB_FIELDS),
LintId::of(pattern_type_mismatch::PATTERN_TYPE_MISMATCH),
LintId::of(pub_use::PUB_USE),
LintId::of(redundant_slicing::DEREF_BY_SLICING),

View file

@ -324,6 +324,7 @@ mod option_if_let_else;
mod overflow_check_conditional;
mod panic_in_result_fn;
mod panic_unimplemented;
mod partial_pub_fields;
mod partialeq_ne_impl;
mod partialeq_to_none;
mod pass_by_ref_or_value;
@ -908,6 +909,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(|_| Box::new(bool_to_int_with_if::BoolToIntWithIf));
store.register_late_pass(|_| Box::new(box_default::BoxDefault));
store.register_late_pass(|_| Box::new(implicit_saturating_add::ImplicitSaturatingAdd));
store.register_early_pass(|| Box::new(partial_pub_fields::PartialPubFields));
// add lints here, do not remove this comment, it's used in `new_lint`
}

View file

@ -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;
}
}
}
}