Skip to content

Commit bf70865

Browse files
committed
Auto merge of #9199 - Xiretza:unused-self-exported-api, r=Jarcho
unused_self: respect avoid-breaking-exported-api ``` changelog: [`unused_self`]: Now respects the `avoid-breaking-exported-api` config option ``` Fixes #9195. I mostly copied the implementation from `unnecessary_wraps`, since I don't have much understanding of rustc internals.
2 parents f4c9183 + 7a5965b commit bf70865

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
782782
))
783783
});
784784
store.register_late_pass(|| Box::new(default::Default::default()));
785-
store.register_late_pass(|| Box::new(unused_self::UnusedSelf));
785+
store.register_late_pass(move || Box::new(unused_self::UnusedSelf::new(avoid_breaking_exported_api)));
786786
store.register_late_pass(|| Box::new(mutable_debug_assertion::DebugAssertWithMutCall));
787787
store.register_late_pass(|| Box::new(exit::Exit));
788788
store.register_late_pass(|| Box::new(to_digit_is_some::ToDigitIsSome));

clippy_lints/src/unused_self.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::visitors::is_local_used;
33
use if_chain::if_chain;
44
use rustc_hir::{Impl, ImplItem, ImplItemKind, ItemKind};
55
use rustc_lint::{LateContext, LateLintPass};
6-
use rustc_session::{declare_lint_pass, declare_tool_lint};
6+
use rustc_session::{declare_tool_lint, impl_lint_pass};
77

88
declare_clippy_lint! {
99
/// ### What it does
@@ -35,7 +35,19 @@ declare_clippy_lint! {
3535
"methods that contain a `self` argument but don't use it"
3636
}
3737

38-
declare_lint_pass!(UnusedSelf => [UNUSED_SELF]);
38+
pub struct UnusedSelf {
39+
avoid_breaking_exported_api: bool,
40+
}
41+
42+
impl_lint_pass!(UnusedSelf => [UNUSED_SELF]);
43+
44+
impl UnusedSelf {
45+
pub fn new(avoid_breaking_exported_api: bool) -> Self {
46+
Self {
47+
avoid_breaking_exported_api,
48+
}
49+
}
50+
}
3951

4052
impl<'tcx> LateLintPass<'tcx> for UnusedSelf {
4153
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &ImplItem<'_>) {
@@ -49,6 +61,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedSelf {
4961
if let ItemKind::Impl(Impl { of_trait: None, .. }) = parent_item.kind;
5062
if assoc_item.fn_has_self_parameter;
5163
if let ImplItemKind::Fn(.., body_id) = &impl_item.kind;
64+
if !cx.access_levels.is_exported(impl_item.def_id) || !self.avoid_breaking_exported_api;
5265
let body = cx.tcx.hir().body(*body_id);
5366
if let [self_param, ..] = body.params;
5467
if !is_local_used(cx, body, self_param.pat.hir_id);

clippy_lints/src/utils/conf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ macro_rules! define_Conf {
191191
}
192192

193193
define_Conf! {
194-
/// Lint: ENUM_VARIANT_NAMES, LARGE_TYPES_PASSED_BY_VALUE, TRIVIALLY_COPY_PASS_BY_REF, UNNECESSARY_WRAPS, UPPER_CASE_ACRONYMS, WRONG_SELF_CONVENTION, BOX_COLLECTION, REDUNDANT_ALLOCATION, RC_BUFFER, VEC_BOX, OPTION_OPTION, LINKEDLIST, RC_MUTEX.
194+
/// Lint: ENUM_VARIANT_NAMES, LARGE_TYPES_PASSED_BY_VALUE, TRIVIALLY_COPY_PASS_BY_REF, UNNECESSARY_WRAPS, UNUSED_SELF, UPPER_CASE_ACRONYMS, WRONG_SELF_CONVENTION, BOX_COLLECTION, REDUNDANT_ALLOCATION, RC_BUFFER, VEC_BOX, OPTION_OPTION, LINKEDLIST, RC_MUTEX.
195195
///
196196
/// Suppress lints whenever the suggested change would cause breakage for other crates.
197197
(avoid_breaking_exported_api: bool = true),

tests/ui/unused_self.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,17 @@ mod unused_self_allow {
5353
// shouldn't trigger
5454
fn unused_self_move(self) {}
5555
}
56+
57+
pub struct D;
58+
59+
impl D {
60+
// shouldn't trigger for public methods
61+
pub fn unused_self_move(self) {}
62+
}
5663
}
5764

65+
pub use unused_self_allow::D;
66+
5867
mod used_self {
5968
use std::pin::Pin;
6069

0 commit comments

Comments
 (0)