Skip to content

Commit b4d76b4

Browse files
committed
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: #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 #7308
2 parents 7bfc26e + c02dcd5 commit b4d76b4

File tree

10 files changed

+167
-88
lines changed

10 files changed

+167
-88
lines changed

clippy_lints/src/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1840,7 +1840,12 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
18401840
store.register_late_pass(|| box serde_api::SerdeApi);
18411841
let vec_box_size_threshold = conf.vec_box_size_threshold;
18421842
let type_complexity_threshold = conf.type_complexity_threshold;
1843-
store.register_late_pass(move || box types::Types::new(vec_box_size_threshold, type_complexity_threshold));
1843+
let avoid_breaking_exported_api = conf.avoid_breaking_exported_api;
1844+
store.register_late_pass(move || box types::Types::new(
1845+
vec_box_size_threshold,
1846+
type_complexity_threshold,
1847+
avoid_breaking_exported_api,
1848+
));
18441849
store.register_late_pass(|| box booleans::NonminimalBool);
18451850
store.register_late_pass(|| box needless_bitwise_bool::NeedlessBitwiseBool);
18461851
store.register_late_pass(|| box eq_op::EqOp);

clippy_lints/src/types/mod.rs

+64-17
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ declare_clippy_lint! {
295295
pub struct Types {
296296
vec_box_size_threshold: u64,
297297
type_complexity_threshold: u64,
298+
avoid_breaking_exported_api: bool,
298299
}
299300

300301
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 {
308309
false
309310
};
310311

312+
let is_exported = cx.access_levels.is_exported(cx.tcx.hir().local_def_id(id));
313+
311314
self.check_fn_decl(
312315
cx,
313316
decl,
314317
CheckTyContext {
315318
is_in_trait_impl,
319+
is_exported,
316320
..CheckTyContext::default()
317321
},
318322
);
319323
}
320324

321325
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
326+
let is_exported = cx.access_levels.is_exported(item.def_id);
327+
322328
match item.kind {
323-
ItemKind::Static(ty, _, _) | ItemKind::Const(ty, _) => self.check_ty(cx, ty, CheckTyContext::default()),
329+
ItemKind::Static(ty, _, _) | ItemKind::Const(ty, _) => self.check_ty(
330+
cx,
331+
ty,
332+
CheckTyContext {
333+
is_exported,
334+
..CheckTyContext::default()
335+
},
336+
),
324337
// functions, enums, structs, impls and traits are covered
325338
_ => (),
326339
}
@@ -342,15 +355,31 @@ impl<'tcx> LateLintPass<'tcx> for Types {
342355
}
343356

344357
fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) {
345-
self.check_ty(cx, field.ty, CheckTyContext::default());
358+
let is_exported = cx.access_levels.is_exported(cx.tcx.hir().local_def_id(field.hir_id));
359+
360+
self.check_ty(
361+
cx,
362+
field.ty,
363+
CheckTyContext {
364+
is_exported,
365+
..CheckTyContext::default()
366+
},
367+
);
346368
}
347369

348-
fn check_trait_item(&mut self, cx: &LateContext<'_>, item: &TraitItem<'_>) {
370+
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &TraitItem<'_>) {
371+
let is_exported = cx.access_levels.is_exported(item.def_id);
372+
373+
let context = CheckTyContext {
374+
is_exported,
375+
..CheckTyContext::default()
376+
};
377+
349378
match item.kind {
350379
TraitItemKind::Const(ty, _) | TraitItemKind::Type(_, Some(ty)) => {
351-
self.check_ty(cx, ty, CheckTyContext::default());
380+
self.check_ty(cx, ty, context);
352381
},
353-
TraitItemKind::Fn(ref sig, _) => self.check_fn_decl(cx, sig.decl, CheckTyContext::default()),
382+
TraitItemKind::Fn(ref sig, _) => self.check_fn_decl(cx, sig.decl, context),
354383
TraitItemKind::Type(..) => (),
355384
}
356385
}
@@ -370,10 +399,11 @@ impl<'tcx> LateLintPass<'tcx> for Types {
370399
}
371400

372401
impl Types {
373-
pub fn new(vec_box_size_threshold: u64, type_complexity_threshold: u64) -> Self {
402+
pub fn new(vec_box_size_threshold: u64, type_complexity_threshold: u64, avoid_breaking_exported_api: bool) -> Self {
374403
Self {
375404
vec_box_size_threshold,
376405
type_complexity_threshold,
406+
avoid_breaking_exported_api,
377407
}
378408
}
379409

@@ -410,17 +440,24 @@ impl Types {
410440
let hir_id = hir_ty.hir_id;
411441
let res = cx.qpath_res(qpath, hir_id);
412442
if let Some(def_id) = res.opt_def_id() {
413-
let mut triggered = false;
414-
triggered |= box_vec::check(cx, hir_ty, qpath, def_id);
415-
triggered |= redundant_allocation::check(cx, hir_ty, qpath, def_id);
416-
triggered |= rc_buffer::check(cx, hir_ty, qpath, def_id);
417-
triggered |= vec_box::check(cx, hir_ty, qpath, def_id, self.vec_box_size_threshold);
418-
triggered |= option_option::check(cx, hir_ty, qpath, def_id);
419-
triggered |= linked_list::check(cx, hir_ty, def_id);
420-
triggered |= rc_mutex::check(cx, hir_ty, qpath, def_id);
421-
422-
if triggered {
423-
return;
443+
if self.is_type_change_allowed(context) {
444+
// All lints that are being checked in this block are guarded by
445+
// the `avoid_breaking_exported_api` configuration. When adding a
446+
// new lint, please also add the name to the configuration documentation
447+
// in `clippy_lints::utils::conf.rs`
448+
449+
let mut triggered = false;
450+
triggered |= box_vec::check(cx, hir_ty, qpath, def_id);
451+
triggered |= redundant_allocation::check(cx, hir_ty, qpath, def_id);
452+
triggered |= rc_buffer::check(cx, hir_ty, qpath, def_id);
453+
triggered |= vec_box::check(cx, hir_ty, qpath, def_id, self.vec_box_size_threshold);
454+
triggered |= option_option::check(cx, hir_ty, qpath, def_id);
455+
triggered |= linked_list::check(cx, hir_ty, def_id);
456+
triggered |= rc_mutex::check(cx, hir_ty, qpath, def_id);
457+
458+
if triggered {
459+
return;
460+
}
424461
}
425462
}
426463
match *qpath {
@@ -487,11 +524,21 @@ impl Types {
487524
_ => {},
488525
}
489526
}
527+
528+
/// This function checks if the type is allowed to change in the current context
529+
/// based on the `avoid_breaking_exported_api` configuration
530+
fn is_type_change_allowed(&self, context: CheckTyContext) -> bool {
531+
!(context.is_exported && self.avoid_breaking_exported_api)
532+
}
490533
}
491534

535+
#[allow(clippy::struct_excessive_bools)]
492536
#[derive(Clone, Copy, Default)]
493537
struct CheckTyContext {
494538
is_in_trait_impl: bool,
539+
/// `true` for types on local variables.
495540
is_local: bool,
541+
/// `true` for types that are part of the public API.
542+
is_exported: bool,
496543
is_nested_call: bool,
497544
}

clippy_lints/src/types/rc_mutex.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint;
1+
use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::is_ty_param_diagnostic_item;
33
use if_chain::if_chain;
44
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<'_
1111
if_chain! {
1212
if cx.tcx.is_diagnostic_item(sym::Rc, def_id) ;
1313
if let Some(_) = is_ty_param_diagnostic_item(cx, qpath, sym!(mutex_type)) ;
14-
15-
then{
16-
span_lint(
14+
then {
15+
span_lint_and_help(
1716
cx,
1817
RC_MUTEX,
1918
hir_ty.span,
20-
"found `Rc<Mutex<_>>`. Consider using `Rc<RefCell<_>>` or `Arc<Mutex<_>>` instead",
19+
"usage of `Rc<Mutex<_>>`",
20+
None,
21+
"consider using `Rc<RefCell<_>>` or `Arc<Mutex<_>>` instead",
2122
);
2223
return true;
2324
}

clippy_lints/src/utils/conf.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ macro_rules! define_Conf {
132132

133133
// N.B., this macro is parsed by util/lintlib.py
134134
define_Conf! {
135-
/// Lint: ENUM_VARIANT_NAMES, LARGE_TYPES_PASSED_BY_VALUE, TRIVIALLY_COPY_PASS_BY_REF, UNNECESSARY_WRAPS, UPPER_CASE_ACRONYMS, WRONG_SELF_CONVENTION.
135+
/// 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.
136136
///
137137
/// Suppress lints whenever the suggested change would cause breakage for other crates.
138138
(avoid_breaking_exported_api: bool = true),

tests/ui/box_vec.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#![warn(clippy::all)]
2-
#![allow(clippy::boxed_local, clippy::needless_pass_by_value)]
3-
#![allow(clippy::blacklisted_name)]
2+
#![allow(
3+
clippy::boxed_local,
4+
clippy::needless_pass_by_value,
5+
clippy::blacklisted_name,
6+
unused
7+
)]
48

59
macro_rules! boxit {
610
($init:expr, $x:ty) => {
@@ -11,22 +15,22 @@ macro_rules! boxit {
1115
fn test_macro() {
1216
boxit!(Vec::new(), Vec<u8>);
1317
}
14-
pub fn test(foo: Box<Vec<bool>>) {
15-
println!("{:?}", foo.get(0))
16-
}
18+
fn test(foo: Box<Vec<bool>>) {}
1719

18-
pub fn test2(foo: Box<dyn Fn(Vec<u32>)>) {
20+
fn test2(foo: Box<dyn Fn(Vec<u32>)>) {
1921
// pass if #31 is fixed
2022
foo(vec![1, 2, 3])
2123
}
2224

23-
pub fn test_local_not_linted() {
25+
fn test_local_not_linted() {
2426
let _: Box<Vec<bool>>;
2527
}
2628

27-
fn main() {
28-
test(Box::new(Vec::new()));
29-
test2(Box::new(|v| println!("{:?}", v)));
30-
test_macro();
31-
test_local_not_linted();
29+
// All of these test should be allowed because they are part of the
30+
// public api and `avoid_breaking_exported_api` is `false` by default.
31+
pub fn pub_test(foo: Box<Vec<bool>>) {}
32+
pub fn pub_test_ret() -> Box<Vec<bool>> {
33+
Box::new(Vec::new())
3234
}
35+
36+
fn main() {}

tests/ui/box_vec.stderr

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

tests/ui/linkedlist.rs

+18-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![feature(associated_type_defaults)]
22
#![warn(clippy::linkedlist)]
3-
#![allow(dead_code, clippy::needless_pass_by_value)]
3+
#![allow(unused, dead_code, clippy::needless_pass_by_value)]
44

55
extern crate alloc;
66
use alloc::collections::linked_list::LinkedList;
@@ -20,24 +20,29 @@ impl Foo for LinkedList<u8> {
2020
const BAR: Option<LinkedList<u8>> = None;
2121
}
2222

23-
struct Bar;
23+
pub struct Bar {
24+
priv_linked_list_field: LinkedList<u8>,
25+
pub pub_linked_list_field: LinkedList<u8>,
26+
}
2427
impl Bar {
2528
fn foo(_: LinkedList<u8>) {}
2629
}
2730

28-
pub fn test(my_favourite_linked_list: LinkedList<u8>) {
29-
println!("{:?}", my_favourite_linked_list)
30-
}
31-
32-
pub fn test_ret() -> Option<LinkedList<u8>> {
33-
unimplemented!();
31+
// All of these test should be trigger the lint because they are not
32+
// part of the public api
33+
fn test(my_favorite_linked_list: LinkedList<u8>) {}
34+
fn test_ret() -> Option<LinkedList<u8>> {
35+
None
3436
}
35-
36-
pub fn test_local_not_linted() {
37+
fn test_local_not_linted() {
3738
let _: LinkedList<u8>;
3839
}
3940

40-
fn main() {
41-
test(LinkedList::new());
42-
test_local_not_linted();
41+
// All of these test should be allowed because they are part of the
42+
// public api and `avoid_breaking_exported_api` is `false` by default.
43+
pub fn pub_test(the_most_awesome_linked_list: LinkedList<u8>) {}
44+
pub fn pub_test_ret() -> Option<LinkedList<u8>> {
45+
None
4346
}
47+
48+
fn main() {}

tests/ui/linkedlist.stderr

+16-8
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,36 @@ LL | const BAR: Option<LinkedList<u8>>;
4040
= help: a `VecDeque` might work
4141

4242
error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure?
43-
--> $DIR/linkedlist.rs:25:15
43+
--> $DIR/linkedlist.rs:24:29
44+
|
45+
LL | priv_linked_list_field: LinkedList<u8>,
46+
| ^^^^^^^^^^^^^^
47+
|
48+
= help: a `VecDeque` might work
49+
50+
error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure?
51+
--> $DIR/linkedlist.rs:28:15
4452
|
4553
LL | fn foo(_: LinkedList<u8>) {}
4654
| ^^^^^^^^^^^^^^
4755
|
4856
= help: a `VecDeque` might work
4957

5058
error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure?
51-
--> $DIR/linkedlist.rs:28:39
59+
--> $DIR/linkedlist.rs:33:34
5260
|
53-
LL | pub fn test(my_favourite_linked_list: LinkedList<u8>) {
54-
| ^^^^^^^^^^^^^^
61+
LL | fn test(my_favorite_linked_list: LinkedList<u8>) {}
62+
| ^^^^^^^^^^^^^^
5563
|
5664
= help: a `VecDeque` might work
5765

5866
error: you seem to be using a `LinkedList`! Perhaps you meant some other data structure?
59-
--> $DIR/linkedlist.rs:32:29
67+
--> $DIR/linkedlist.rs:34:25
6068
|
61-
LL | pub fn test_ret() -> Option<LinkedList<u8>> {
62-
| ^^^^^^^^^^^^^^
69+
LL | fn test_ret() -> Option<LinkedList<u8>> {
70+
| ^^^^^^^^^^^^^^
6371
|
6472
= help: a `VecDeque` might work
6573

66-
error: aborting due to 8 previous errors
74+
error: aborting due to 9 previous errors
6775

tests/ui/rc_mutex.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
#![warn(clippy::rc_mutex)]
2-
#![allow(clippy::blacklisted_name)]
2+
#![allow(unused, clippy::blacklisted_name)]
33

44
use std::rc::Rc;
55
use std::sync::Mutex;
66

7-
pub struct MyStruct {
7+
pub struct MyStructWithPrivItem {
88
foo: Rc<Mutex<i32>>,
99
}
1010

11+
pub struct MyStructWithPubItem {
12+
pub foo: Rc<Mutex<i32>>,
13+
}
14+
1115
pub struct SubT<T> {
1216
foo: T,
1317
}
@@ -17,18 +21,16 @@ pub enum MyEnum {
1721
Two,
1822
}
1923

20-
pub fn test1<T>(foo: Rc<Mutex<T>>) {}
21-
22-
pub fn test2(foo: Rc<Mutex<MyEnum>>) {}
24+
// All of these test should be trigger the lint because they are not
25+
// part of the public api
26+
fn test1<T>(foo: Rc<Mutex<T>>) {}
27+
fn test2(foo: Rc<Mutex<MyEnum>>) {}
28+
fn test3(foo: Rc<Mutex<SubT<usize>>>) {}
2329

24-
pub fn test3(foo: Rc<Mutex<SubT<usize>>>) {}
30+
// All of these test should be allowed because they are part of the
31+
// public api and `avoid_breaking_exported_api` is `false` by default.
32+
pub fn pub_test1<T>(foo: Rc<Mutex<T>>) {}
33+
pub fn pub_test2(foo: Rc<Mutex<MyEnum>>) {}
34+
pub fn pub_test3(foo: Rc<Mutex<SubT<usize>>>) {}
2535

26-
fn main() {
27-
test1(Rc::new(Mutex::new(1)));
28-
test2(Rc::new(Mutex::new(MyEnum::One)));
29-
test3(Rc::new(Mutex::new(SubT { foo: 1 })));
30-
31-
let _my_struct = MyStruct {
32-
foo: Rc::new(Mutex::new(1)),
33-
};
34-
}
36+
fn main() {}

0 commit comments

Comments
 (0)