Skip to content

Commit d7f47f2

Browse files
committed
Use break api config for wrong_pub_self_convention
1 parent 2e2021b commit d7f47f2

10 files changed

+55
-80
lines changed

clippy_lints/src/deprecated_lints.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,12 @@ declare_deprecated_lint! {
141141
pub FILTER_MAP,
142142
"this lint has been replaced by `manual_filter_map`, a more specific lint"
143143
}
144+
145+
declare_deprecated_lint! {
146+
/// **What it does:** Nothing. This lint has been deprecated.
147+
///
148+
/// **Deprecation reason:** The `avoid_breaking_exported_api` config option was added, which
149+
/// enables the `wrong_self_conversion` lint for public items.
150+
pub WRONG_PUB_SELF_CONVENTION,
151+
"set the `avoid_breaking_exported_api` config option to `false` to enable the `wrong_self_convention` lint for public items"
152+
}

clippy_lints/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,10 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
505505
"clippy::filter_map",
506506
"this lint has been replaced by `manual_filter_map`, a more specific lint",
507507
);
508+
store.register_removed(
509+
"clippy::wrong_pub_self_convention",
510+
"set the `avoid_breaking_exported_api` config option to `false` to enable the `wrong_self_convention` lint for public items",
511+
);
508512
// end deprecated lints, do not remove this comment, it’s used in `update_lints`
509513

510514
// begin register lints, do not remove this comment, it’s used in `update_lints`
@@ -802,7 +806,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
802806
methods::UNNECESSARY_LAZY_EVALUATIONS,
803807
methods::UNWRAP_USED,
804808
methods::USELESS_ASREF,
805-
methods::WRONG_PUB_SELF_CONVENTION,
806809
methods::WRONG_SELF_CONVENTION,
807810
methods::ZST_OFFSET,
808811
minmax::MIN_MAX,
@@ -1026,7 +1029,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10261029
LintId::of(methods::FILETYPE_IS_FILE),
10271030
LintId::of(methods::GET_UNWRAP),
10281031
LintId::of(methods::UNWRAP_USED),
1029-
LintId::of(methods::WRONG_PUB_SELF_CONVENTION),
10301032
LintId::of(misc::FLOAT_CMP_CONST),
10311033
LintId::of(misc_early::UNNEEDED_FIELD_PATTERN),
10321034
LintId::of(missing_doc::MISSING_DOCS_IN_PRIVATE_ITEMS),
@@ -1862,7 +1864,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
18621864
})
18631865
});
18641866

1865-
store.register_late_pass(move || box methods::Methods::new(msrv));
1867+
let avoid_breaking_exported_api = conf.avoid_breaking_exported_api;
1868+
store.register_late_pass(move || box methods::Methods::new(avoid_breaking_exported_api, msrv));
18661869
store.register_late_pass(move || box matches::Matches::new(msrv));
18671870
store.register_early_pass(move || box manual_non_exhaustive::ManualNonExhaustive::new(msrv));
18681871
store.register_late_pass(move || box manual_strip::ManualStrip::new(msrv));

clippy_lints/src/methods/mod.rs

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -282,30 +282,6 @@ declare_clippy_lint! {
282282
"defining a method named with an established prefix (like \"into_\") that takes `self` with the wrong convention"
283283
}
284284

285-
declare_clippy_lint! {
286-
/// **What it does:** This is the same as
287-
/// [`wrong_self_convention`](#wrong_self_convention), but for public items.
288-
///
289-
/// **Why is this bad?** See [`wrong_self_convention`](#wrong_self_convention).
290-
///
291-
/// **Known problems:** Actually *renaming* the function may break clients if
292-
/// the function is part of the public interface. In that case, be mindful of
293-
/// the stability guarantees you've given your users.
294-
///
295-
/// **Example:**
296-
/// ```rust
297-
/// # struct X;
298-
/// impl<'a> X {
299-
/// pub fn as_str(self) -> &'a str {
300-
/// "foo"
301-
/// }
302-
/// }
303-
/// ```
304-
pub WRONG_PUB_SELF_CONVENTION,
305-
restriction,
306-
"defining a public method named with an established prefix (like \"into_\") that takes `self` with the wrong convention"
307-
}
308-
309285
declare_clippy_lint! {
310286
/// **What it does:** Checks for usage of `ok().expect(..)`.
311287
///
@@ -1658,13 +1634,17 @@ declare_clippy_lint! {
16581634
}
16591635

16601636
pub struct Methods {
1637+
avoid_breaking_exported_api: bool,
16611638
msrv: Option<RustcVersion>,
16621639
}
16631640

16641641
impl Methods {
16651642
#[must_use]
1666-
pub fn new(msrv: Option<RustcVersion>) -> Self {
1667-
Self { msrv }
1643+
pub fn new(avoid_breaking_exported_api: bool, msrv: Option<RustcVersion>) -> Self {
1644+
Self {
1645+
avoid_breaking_exported_api,
1646+
msrv,
1647+
}
16681648
}
16691649
}
16701650

@@ -1673,7 +1653,6 @@ impl_lint_pass!(Methods => [
16731653
EXPECT_USED,
16741654
SHOULD_IMPLEMENT_TRAIT,
16751655
WRONG_SELF_CONVENTION,
1676-
WRONG_PUB_SELF_CONVENTION,
16771656
OK_EXPECT,
16781657
MAP_UNWRAP_OR,
16791658
RESULT_MAP_OR_INTO_OPTION,
@@ -1838,11 +1817,13 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
18381817
}
18391818
}
18401819

1841-
if sig.decl.implicit_self.has_implicit_self() {
1820+
if sig.decl.implicit_self.has_implicit_self()
1821+
&& !(self.avoid_breaking_exported_api
1822+
&& cx.access_levels.is_exported(impl_item.hir_id()))
1823+
{
18421824
wrong_self_convention::check(
18431825
cx,
18441826
&name,
1845-
item.vis.node.is_pub(),
18461827
self_ty,
18471828
first_arg_ty,
18481829
first_arg.pat.span,
@@ -1915,7 +1896,6 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
19151896
wrong_self_convention::check(
19161897
cx,
19171898
&item.ident.name.as_str(),
1918-
false,
19191899
self_ty,
19201900
first_arg_ty,
19211901
first_arg_span,

clippy_lints/src/methods/wrong_self_convention.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use rustc_middle::ty::TyS;
66
use rustc_span::source_map::Span;
77
use std::fmt;
88

9-
use super::WRONG_PUB_SELF_CONVENTION;
109
use super::WRONG_SELF_CONVENTION;
1110

1211
#[rustfmt::skip]
@@ -21,9 +20,9 @@ const CONVENTIONS: [(&[Convention], &[SelfKind]); 9] = [
2120

2221
// Conversion using `to_` can use borrowed (non-Copy types) or owned (Copy types).
2322
// Source: https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv
24-
(&[Convention::StartsWith("to_"), Convention::NotEndsWith("_mut"), Convention::IsSelfTypeCopy(false),
23+
(&[Convention::StartsWith("to_"), Convention::NotEndsWith("_mut"), Convention::IsSelfTypeCopy(false),
2524
Convention::IsTraitItem(false), Convention::ImplementsTrait(false)], &[SelfKind::Ref]),
26-
(&[Convention::StartsWith("to_"), Convention::NotEndsWith("_mut"), Convention::IsSelfTypeCopy(true),
25+
(&[Convention::StartsWith("to_"), Convention::NotEndsWith("_mut"), Convention::IsSelfTypeCopy(true),
2726
Convention::IsTraitItem(false), Convention::ImplementsTrait(false)], &[SelfKind::Value]),
2827
];
2928

@@ -85,18 +84,12 @@ impl fmt::Display for Convention {
8584
pub(super) fn check<'tcx>(
8685
cx: &LateContext<'tcx>,
8786
item_name: &str,
88-
is_pub: bool,
8987
self_ty: &'tcx TyS<'tcx>,
9088
first_arg_ty: &'tcx TyS<'tcx>,
9189
first_arg_span: Span,
9290
implements_trait: bool,
9391
is_trait_item: bool,
9492
) {
95-
let lint = if is_pub {
96-
WRONG_PUB_SELF_CONVENTION
97-
} else {
98-
WRONG_SELF_CONVENTION
99-
};
10093
if let Some((conventions, self_kinds)) = &CONVENTIONS.iter().find(|(convs, _)| {
10194
convs
10295
.iter()
@@ -142,7 +135,7 @@ pub(super) fn check<'tcx>(
142135

143136
span_lint_and_help(
144137
cx,
145-
lint,
138+
WRONG_SELF_CONVENTION,
146139
first_arg_span,
147140
&format!(
148141
"{} usually take {}",

tests/ui/def_id_nocore.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn start(_argc: isize, _argv: *const *const u8) -> isize {
2020
0
2121
}
2222

23-
pub struct A;
23+
struct A;
2424

2525
impl A {
2626
pub fn as_ref(self) -> &'static str {

tests/ui/module_name_repetitions.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,4 @@ mod foo {
1515
pub struct Foobar;
1616
}
1717

18-
#[cfg(test)]
19-
mod test {
20-
#[test]
21-
fn it_works() {
22-
assert_eq!(2 + 2, 4);
23-
}
24-
}
25-
2618
fn main() {}

tests/ui/wrong_self_convention.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// edition:2018
22
#![warn(clippy::wrong_self_convention)]
3-
#![warn(clippy::wrong_pub_self_convention)]
43
#![allow(dead_code)]
54

65
fn main() {}

0 commit comments

Comments
 (0)