Auto merge of #7560 - xFrednet:7289-configuration-for-every-type-lint, r=camsteffen

Use `avoid-breaking-exported-api` configuration in types module

This PR empowers our lovely `avoid-breaking-exported-api` configuration value to also influence the emission of lints inside the `types` module.

(That's pretty much it, not really a change worthy of writing a fairy tale about. Don't get me wrong, I would love to write a short one, but I sadly need to study now).

---

Closes: rust-lang/rust-clippy#7489

changelog: The `avoid-breaking-exported-api` configuration now also works for [`box_vec`], [`redundant_allocation`], [`rc_buffer`], [`vec_box`], [`option_option`], [`linkedlist`], [`rc_mutex`]

changelog: [`rc_mutex`]: update the lint message to comply with the normal format

---

r? `@camsteffen,` as you implemented the configuration value

cc: `@flip1995,` as we've discussed this change in rust-lang/rust-clippy#7308
This commit is contained in:
bors 2021-08-12 20:20:58 +00:00
commit b4d76b42fd
10 changed files with 166 additions and 87 deletions

View file

@ -1840,7 +1840,12 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(|| box serde_api::SerdeApi); store.register_late_pass(|| box serde_api::SerdeApi);
let vec_box_size_threshold = conf.vec_box_size_threshold; let vec_box_size_threshold = conf.vec_box_size_threshold;
let type_complexity_threshold = conf.type_complexity_threshold; let type_complexity_threshold = conf.type_complexity_threshold;
store.register_late_pass(move || box types::Types::new(vec_box_size_threshold, type_complexity_threshold)); let avoid_breaking_exported_api = conf.avoid_breaking_exported_api;
store.register_late_pass(move || box types::Types::new(
vec_box_size_threshold,
type_complexity_threshold,
avoid_breaking_exported_api,
));
store.register_late_pass(|| box booleans::NonminimalBool); store.register_late_pass(|| box booleans::NonminimalBool);
store.register_late_pass(|| box needless_bitwise_bool::NeedlessBitwiseBool); store.register_late_pass(|| box needless_bitwise_bool::NeedlessBitwiseBool);
store.register_late_pass(|| box eq_op::EqOp); store.register_late_pass(|| box eq_op::EqOp);

View file

@ -295,6 +295,7 @@ declare_clippy_lint! {
pub struct Types { pub struct Types {
vec_box_size_threshold: u64, vec_box_size_threshold: u64,
type_complexity_threshold: u64, type_complexity_threshold: u64,
avoid_breaking_exported_api: bool,
} }
impl_lint_pass!(Types => [BOX_VEC, VEC_BOX, OPTION_OPTION, LINKEDLIST, BORROWED_BOX, REDUNDANT_ALLOCATION, RC_BUFFER, RC_MUTEX, TYPE_COMPLEXITY]); impl_lint_pass!(Types => [BOX_VEC, VEC_BOX, OPTION_OPTION, LINKEDLIST, BORROWED_BOX, REDUNDANT_ALLOCATION, RC_BUFFER, RC_MUTEX, TYPE_COMPLEXITY]);
@ -308,19 +309,31 @@ impl<'tcx> LateLintPass<'tcx> for Types {
false false
}; };
let is_exported = cx.access_levels.is_exported(cx.tcx.hir().local_def_id(id));
self.check_fn_decl( self.check_fn_decl(
cx, cx,
decl, decl,
CheckTyContext { CheckTyContext {
is_in_trait_impl, is_in_trait_impl,
is_exported,
..CheckTyContext::default() ..CheckTyContext::default()
}, },
); );
} }
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) { fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
let is_exported = cx.access_levels.is_exported(item.def_id);
match item.kind { match item.kind {
ItemKind::Static(ty, _, _) | ItemKind::Const(ty, _) => self.check_ty(cx, ty, CheckTyContext::default()), ItemKind::Static(ty, _, _) | ItemKind::Const(ty, _) => self.check_ty(
cx,
ty,
CheckTyContext {
is_exported,
..CheckTyContext::default()
},
),
// functions, enums, structs, impls and traits are covered // functions, enums, structs, impls and traits are covered
_ => (), _ => (),
} }
@ -342,15 +355,31 @@ impl<'tcx> LateLintPass<'tcx> for Types {
} }
fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) { fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) {
self.check_ty(cx, field.ty, CheckTyContext::default()); let is_exported = cx.access_levels.is_exported(cx.tcx.hir().local_def_id(field.hir_id));
self.check_ty(
cx,
field.ty,
CheckTyContext {
is_exported,
..CheckTyContext::default()
},
);
} }
fn check_trait_item(&mut self, cx: &LateContext<'_>, item: &TraitItem<'_>) { fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &TraitItem<'_>) {
let is_exported = cx.access_levels.is_exported(item.def_id);
let context = CheckTyContext {
is_exported,
..CheckTyContext::default()
};
match item.kind { match item.kind {
TraitItemKind::Const(ty, _) | TraitItemKind::Type(_, Some(ty)) => { TraitItemKind::Const(ty, _) | TraitItemKind::Type(_, Some(ty)) => {
self.check_ty(cx, ty, CheckTyContext::default()); self.check_ty(cx, ty, context);
}, },
TraitItemKind::Fn(ref sig, _) => self.check_fn_decl(cx, sig.decl, CheckTyContext::default()), TraitItemKind::Fn(ref sig, _) => self.check_fn_decl(cx, sig.decl, context),
TraitItemKind::Type(..) => (), TraitItemKind::Type(..) => (),
} }
} }
@ -370,10 +399,11 @@ impl<'tcx> LateLintPass<'tcx> for Types {
} }
impl Types { impl Types {
pub fn new(vec_box_size_threshold: u64, type_complexity_threshold: u64) -> Self { pub fn new(vec_box_size_threshold: u64, type_complexity_threshold: u64, avoid_breaking_exported_api: bool) -> Self {
Self { Self {
vec_box_size_threshold, vec_box_size_threshold,
type_complexity_threshold, type_complexity_threshold,
avoid_breaking_exported_api,
} }
} }
@ -410,17 +440,24 @@ impl Types {
let hir_id = hir_ty.hir_id; let hir_id = hir_ty.hir_id;
let res = cx.qpath_res(qpath, hir_id); let res = cx.qpath_res(qpath, hir_id);
if let Some(def_id) = res.opt_def_id() { if let Some(def_id) = res.opt_def_id() {
let mut triggered = false; if self.is_type_change_allowed(context) {
triggered |= box_vec::check(cx, hir_ty, qpath, def_id); // All lints that are being checked in this block are guarded by
triggered |= redundant_allocation::check(cx, hir_ty, qpath, def_id); // the `avoid_breaking_exported_api` configuration. When adding a
triggered |= rc_buffer::check(cx, hir_ty, qpath, def_id); // new lint, please also add the name to the configuration documentation
triggered |= vec_box::check(cx, hir_ty, qpath, def_id, self.vec_box_size_threshold); // in `clippy_lints::utils::conf.rs`
triggered |= option_option::check(cx, hir_ty, qpath, def_id);
triggered |= linked_list::check(cx, hir_ty, def_id);
triggered |= rc_mutex::check(cx, hir_ty, qpath, def_id);
if triggered { let mut triggered = false;
return; triggered |= box_vec::check(cx, hir_ty, qpath, def_id);
triggered |= redundant_allocation::check(cx, hir_ty, qpath, def_id);
triggered |= rc_buffer::check(cx, hir_ty, qpath, def_id);
triggered |= vec_box::check(cx, hir_ty, qpath, def_id, self.vec_box_size_threshold);
triggered |= option_option::check(cx, hir_ty, qpath, def_id);
triggered |= linked_list::check(cx, hir_ty, def_id);
triggered |= rc_mutex::check(cx, hir_ty, qpath, def_id);
if triggered {
return;
}
} }
} }
match *qpath { match *qpath {
@ -487,11 +524,21 @@ impl Types {
_ => {}, _ => {},
} }
} }
/// This function checks if the type is allowed to change in the current context
/// based on the `avoid_breaking_exported_api` configuration
fn is_type_change_allowed(&self, context: CheckTyContext) -> bool {
!(context.is_exported && self.avoid_breaking_exported_api)
}
} }
#[allow(clippy::struct_excessive_bools)]
#[derive(Clone, Copy, Default)] #[derive(Clone, Copy, Default)]
struct CheckTyContext { struct CheckTyContext {
is_in_trait_impl: bool, is_in_trait_impl: bool,
/// `true` for types on local variables.
is_local: bool, is_local: bool,
/// `true` for types that are part of the public API.
is_exported: bool,
is_nested_call: bool, is_nested_call: bool,
} }

View file

@ -1,4 +1,4 @@
use clippy_utils::diagnostics::span_lint; use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::is_ty_param_diagnostic_item; use clippy_utils::is_ty_param_diagnostic_item;
use if_chain::if_chain; use if_chain::if_chain;
use rustc_hir::{self as hir, def_id::DefId, QPath}; use rustc_hir::{self as hir, def_id::DefId, QPath};
@ -11,13 +11,14 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
if_chain! { if_chain! {
if cx.tcx.is_diagnostic_item(sym::Rc, def_id) ; if cx.tcx.is_diagnostic_item(sym::Rc, def_id) ;
if let Some(_) = is_ty_param_diagnostic_item(cx, qpath, sym!(mutex_type)) ; if let Some(_) = is_ty_param_diagnostic_item(cx, qpath, sym!(mutex_type)) ;
then {
then{ span_lint_and_help(
span_lint(
cx, cx,
RC_MUTEX, RC_MUTEX,
hir_ty.span, hir_ty.span,
"found `Rc<Mutex<_>>`. Consider using `Rc<RefCell<_>>` or `Arc<Mutex<_>>` instead", "usage of `Rc<Mutex<_>>`",
None,
"consider using `Rc<RefCell<_>>` or `Arc<Mutex<_>>` instead",
); );
return true; return true;
} }

View file

@ -132,7 +132,7 @@ macro_rules! define_Conf {
// N.B., this macro is parsed by util/lintlib.py // N.B., this macro is parsed by util/lintlib.py
define_Conf! { define_Conf! {
/// Lint: ENUM_VARIANT_NAMES, LARGE_TYPES_PASSED_BY_VALUE, TRIVIALLY_COPY_PASS_BY_REF, UNNECESSARY_WRAPS, UPPER_CASE_ACRONYMS, WRONG_SELF_CONVENTION. /// Lint: ENUM_VARIANT_NAMES, LARGE_TYPES_PASSED_BY_VALUE, TRIVIALLY_COPY_PASS_BY_REF, UNNECESSARY_WRAPS, UPPER_CASE_ACRONYMS, WRONG_SELF_CONVENTION, BOX_VEC, REDUNDANT_ALLOCATION, RC_BUFFER, VEC_BOX, OPTION_OPTION, LINKEDLIST, RC_MUTEX.
/// ///
/// Suppress lints whenever the suggested change would cause breakage for other crates. /// Suppress lints whenever the suggested change would cause breakage for other crates.
(avoid_breaking_exported_api: bool = true), (avoid_breaking_exported_api: bool = true),

View file

@ -1,6 +1,10 @@
#![warn(clippy::all)] #![warn(clippy::all)]
#![allow(clippy::boxed_local, clippy::needless_pass_by_value)] #![allow(
#![allow(clippy::blacklisted_name)] clippy::boxed_local,
clippy::needless_pass_by_value,
clippy::blacklisted_name,
unused
)]
macro_rules! boxit { macro_rules! boxit {
($init:expr, $x:ty) => { ($init:expr, $x:ty) => {
@ -11,22 +15,22 @@ macro_rules! boxit {
fn test_macro() { fn test_macro() {
boxit!(Vec::new(), Vec<u8>); boxit!(Vec::new(), Vec<u8>);
} }
pub fn test(foo: Box<Vec<bool>>) { fn test(foo: Box<Vec<bool>>) {}
println!("{:?}", foo.get(0))
}
pub fn test2(foo: Box<dyn Fn(Vec<u32>)>) { fn test2(foo: Box<dyn Fn(Vec<u32>)>) {
// pass if #31 is fixed // pass if #31 is fixed
foo(vec![1, 2, 3]) foo(vec![1, 2, 3])
} }
pub fn test_local_not_linted() { fn test_local_not_linted() {
let _: Box<Vec<bool>>; let _: Box<Vec<bool>>;
} }
fn main() { // All of these test should be allowed because they are part of the
test(Box::new(Vec::new())); // public api and `avoid_breaking_exported_api` is `false` by default.
test2(Box::new(|v| println!("{:?}", v))); pub fn pub_test(foo: Box<Vec<bool>>) {}
test_macro(); pub fn pub_test_ret() -> Box<Vec<bool>> {
test_local_not_linted(); Box::new(Vec::new())
} }
fn main() {}

View file

@ -1,8 +1,8 @@
error: you seem to be trying to use `Box<Vec<T>>`. Consider using just `Vec<T>` error: you seem to be trying to use `Box<Vec<T>>`. Consider using just `Vec<T>`
--> $DIR/box_vec.rs:14:18 --> $DIR/box_vec.rs:18:14
| |
LL | pub fn test(foo: Box<Vec<bool>>) { LL | fn test(foo: Box<Vec<bool>>) {}
| ^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^
| |
= note: `-D clippy::box-vec` implied by `-D warnings` = note: `-D clippy::box-vec` implied by `-D warnings`
= help: `Vec<T>` is already on the heap, `Box<Vec<T>>` makes an extra allocation = help: `Vec<T>` is already on the heap, `Box<Vec<T>>` makes an extra allocation

View file

@ -1,6 +1,6 @@
#![feature(associated_type_defaults)] #![feature(associated_type_defaults)]
#![warn(clippy::linkedlist)] #![warn(clippy::linkedlist)]
#![allow(dead_code, clippy::needless_pass_by_value)] #![allow(unused, dead_code, clippy::needless_pass_by_value)]
extern crate alloc; extern crate alloc;
use alloc::collections::linked_list::LinkedList; use alloc::collections::linked_list::LinkedList;
@ -20,24 +20,29 @@ impl Foo for LinkedList<u8> {
const BAR: Option<LinkedList<u8>> = None; const BAR: Option<LinkedList<u8>> = None;
} }
struct Bar; pub struct Bar {
priv_linked_list_field: LinkedList<u8>,
pub pub_linked_list_field: LinkedList<u8>,
}
impl Bar { impl Bar {
fn foo(_: LinkedList<u8>) {} fn foo(_: LinkedList<u8>) {}
} }
pub fn test(my_favourite_linked_list: LinkedList<u8>) { // All of these test should be trigger the lint because they are not
println!("{:?}", my_favourite_linked_list) // part of the public api
fn test(my_favorite_linked_list: LinkedList<u8>) {}
fn test_ret() -> Option<LinkedList<u8>> {
None
} }
fn test_local_not_linted() {
pub fn test_ret() -> Option<LinkedList<u8>> {
unimplemented!();
}
pub fn test_local_not_linted() {
let _: LinkedList<u8>; let _: LinkedList<u8>;
} }
fn main() { // All of these test should be allowed because they are part of the
test(LinkedList::new()); // public api and `avoid_breaking_exported_api` is `false` by default.
test_local_not_linted(); pub fn pub_test(the_most_awesome_linked_list: LinkedList<u8>) {}
pub fn pub_test_ret() -> Option<LinkedList<u8>> {
None
} }
fn main() {}

View file

@ -40,7 +40,15 @@ LL | const BAR: Option<LinkedList<u8>>;
= help: a `VecDeque` might work = help: a `VecDeque` might work
error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure? error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure?
--> $DIR/linkedlist.rs:25:15 --> $DIR/linkedlist.rs:24:29
|
LL | priv_linked_list_field: LinkedList<u8>,
| ^^^^^^^^^^^^^^
|
= help: a `VecDeque` might work
error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure?
--> $DIR/linkedlist.rs:28:15
| |
LL | fn foo(_: LinkedList<u8>) {} LL | fn foo(_: LinkedList<u8>) {}
| ^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^
@ -48,20 +56,20 @@ LL | fn foo(_: LinkedList<u8>) {}
= help: a `VecDeque` might work = help: a `VecDeque` might work
error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure? error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure?
--> $DIR/linkedlist.rs:28:39 --> $DIR/linkedlist.rs:33:34
| |
LL | pub fn test(my_favourite_linked_list: LinkedList<u8>) { LL | fn test(my_favorite_linked_list: LinkedList<u8>) {}
| ^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^
| |
= help: a `VecDeque` might work = help: a `VecDeque` might work
error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure? error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure?
--> $DIR/linkedlist.rs:32:29 --> $DIR/linkedlist.rs:34:25
| |
LL | pub fn test_ret() -> Option<LinkedList<u8>> { LL | fn test_ret() -> Option<LinkedList<u8>> {
| ^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^
| |
= help: a `VecDeque` might work = help: a `VecDeque` might work
error: aborting due to 8 previous errors error: aborting due to 9 previous errors

View file

@ -1,13 +1,17 @@
#![warn(clippy::rc_mutex)] #![warn(clippy::rc_mutex)]
#![allow(clippy::blacklisted_name)] #![allow(unused, clippy::blacklisted_name)]
use std::rc::Rc; use std::rc::Rc;
use std::sync::Mutex; use std::sync::Mutex;
pub struct MyStruct { pub struct MyStructWithPrivItem {
foo: Rc<Mutex<i32>>, foo: Rc<Mutex<i32>>,
} }
pub struct MyStructWithPubItem {
pub foo: Rc<Mutex<i32>>,
}
pub struct SubT<T> { pub struct SubT<T> {
foo: T, foo: T,
} }
@ -17,18 +21,16 @@ pub enum MyEnum {
Two, Two,
} }
pub fn test1<T>(foo: Rc<Mutex<T>>) {} // All of these test should be trigger the lint because they are not
// part of the public api
fn test1<T>(foo: Rc<Mutex<T>>) {}
fn test2(foo: Rc<Mutex<MyEnum>>) {}
fn test3(foo: Rc<Mutex<SubT<usize>>>) {}
pub fn test2(foo: Rc<Mutex<MyEnum>>) {} // All of these test should be allowed because they are part of the
// public api and `avoid_breaking_exported_api` is `false` by default.
pub fn pub_test1<T>(foo: Rc<Mutex<T>>) {}
pub fn pub_test2(foo: Rc<Mutex<MyEnum>>) {}
pub fn pub_test3(foo: Rc<Mutex<SubT<usize>>>) {}
pub fn test3(foo: Rc<Mutex<SubT<usize>>>) {} fn main() {}
fn main() {
test1(Rc::new(Mutex::new(1)));
test2(Rc::new(Mutex::new(MyEnum::One)));
test3(Rc::new(Mutex::new(SubT { foo: 1 })));
let _my_struct = MyStruct {
foo: Rc::new(Mutex::new(1)),
};
}

View file

@ -1,28 +1,35 @@
error: found `Rc<Mutex<_>>`. Consider using `Rc<RefCell<_>>` or `Arc<Mutex<_>>` instead error: usage of `Rc<Mutex<_>>`
--> $DIR/rc_mutex.rs:8:10 --> $DIR/rc_mutex.rs:8:10
| |
LL | foo: Rc<Mutex<i32>>, LL | foo: Rc<Mutex<i32>>,
| ^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^
| |
= note: `-D clippy::rc-mutex` implied by `-D warnings` = note: `-D clippy::rc-mutex` implied by `-D warnings`
= help: consider using `Rc<RefCell<_>>` or `Arc<Mutex<_>>` instead
error: found `Rc<Mutex<_>>`. Consider using `Rc<RefCell<_>>` or `Arc<Mutex<_>>` instead error: usage of `Rc<Mutex<_>>`
--> $DIR/rc_mutex.rs:20:22 --> $DIR/rc_mutex.rs:26:18
| |
LL | pub fn test1<T>(foo: Rc<Mutex<T>>) {} LL | fn test1<T>(foo: Rc<Mutex<T>>) {}
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
|
= help: consider using `Rc<RefCell<_>>` or `Arc<Mutex<_>>` instead
error: found `Rc<Mutex<_>>`. Consider using `Rc<RefCell<_>>` or `Arc<Mutex<_>>` instead error: usage of `Rc<Mutex<_>>`
--> $DIR/rc_mutex.rs:22:19 --> $DIR/rc_mutex.rs:27:15
| |
LL | pub fn test2(foo: Rc<Mutex<MyEnum>>) {} LL | fn test2(foo: Rc<Mutex<MyEnum>>) {}
| ^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^
|
= help: consider using `Rc<RefCell<_>>` or `Arc<Mutex<_>>` instead
error: found `Rc<Mutex<_>>`. Consider using `Rc<RefCell<_>>` or `Arc<Mutex<_>>` instead error: usage of `Rc<Mutex<_>>`
--> $DIR/rc_mutex.rs:24:19 --> $DIR/rc_mutex.rs:28:15
| |
LL | pub fn test3(foo: Rc<Mutex<SubT<usize>>>) {} LL | fn test3(foo: Rc<Mutex<SubT<usize>>>) {}
| ^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider using `Rc<RefCell<_>>` or `Arc<Mutex<_>>` instead
error: aborting due to 4 previous errors error: aborting due to 4 previous errors