Navigation Menu

Skip to content

Commit

Permalink
Use break api config for wrong_pub_self_convention
Browse files Browse the repository at this point in the history
  • Loading branch information
camsteffen committed May 26, 2021
1 parent 2e2021b commit d7f47f2
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 80 deletions.
9 changes: 9 additions & 0 deletions clippy_lints/src/deprecated_lints.rs
Expand Up @@ -141,3 +141,12 @@ declare_deprecated_lint! {
pub FILTER_MAP,
"this lint has been replaced by `manual_filter_map`, a more specific lint"
}

declare_deprecated_lint! {
/// **What it does:** Nothing. This lint has been deprecated.
///
/// **Deprecation reason:** The `avoid_breaking_exported_api` config option was added, which
/// enables the `wrong_self_conversion` lint for public items.
pub WRONG_PUB_SELF_CONVENTION,
"set the `avoid_breaking_exported_api` config option to `false` to enable the `wrong_self_convention` lint for public items"
}
9 changes: 6 additions & 3 deletions clippy_lints/src/lib.rs
Expand Up @@ -505,6 +505,10 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
"clippy::filter_map",
"this lint has been replaced by `manual_filter_map`, a more specific lint",
);
store.register_removed(
"clippy::wrong_pub_self_convention",
"set the `avoid_breaking_exported_api` config option to `false` to enable the `wrong_self_convention` lint for public items",
);
// end deprecated lints, do not remove this comment, it’s used in `update_lints`

// begin register lints, do not remove this comment, it’s used in `update_lints`
Expand Down Expand Up @@ -802,7 +806,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
methods::UNNECESSARY_LAZY_EVALUATIONS,
methods::UNWRAP_USED,
methods::USELESS_ASREF,
methods::WRONG_PUB_SELF_CONVENTION,
methods::WRONG_SELF_CONVENTION,
methods::ZST_OFFSET,
minmax::MIN_MAX,
Expand Down Expand Up @@ -1026,7 +1029,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(methods::FILETYPE_IS_FILE),
LintId::of(methods::GET_UNWRAP),
LintId::of(methods::UNWRAP_USED),
LintId::of(methods::WRONG_PUB_SELF_CONVENTION),
LintId::of(misc::FLOAT_CMP_CONST),
LintId::of(misc_early::UNNEEDED_FIELD_PATTERN),
LintId::of(missing_doc::MISSING_DOCS_IN_PRIVATE_ITEMS),
Expand Down Expand Up @@ -1862,7 +1864,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
})
});

store.register_late_pass(move || box methods::Methods::new(msrv));
let avoid_breaking_exported_api = conf.avoid_breaking_exported_api;
store.register_late_pass(move || box methods::Methods::new(avoid_breaking_exported_api, msrv));
store.register_late_pass(move || box matches::Matches::new(msrv));
store.register_early_pass(move || box manual_non_exhaustive::ManualNonExhaustive::new(msrv));
store.register_late_pass(move || box manual_strip::ManualStrip::new(msrv));
Expand Down
40 changes: 10 additions & 30 deletions clippy_lints/src/methods/mod.rs
Expand Up @@ -282,30 +282,6 @@ declare_clippy_lint! {
"defining a method named with an established prefix (like \"into_\") that takes `self` with the wrong convention"
}

declare_clippy_lint! {
/// **What it does:** This is the same as
/// [`wrong_self_convention`](#wrong_self_convention), but for public items.
///
/// **Why is this bad?** See [`wrong_self_convention`](#wrong_self_convention).
///
/// **Known problems:** Actually *renaming* the function may break clients if
/// the function is part of the public interface. In that case, be mindful of
/// the stability guarantees you've given your users.
///
/// **Example:**
/// ```rust
/// # struct X;
/// impl<'a> X {
/// pub fn as_str(self) -> &'a str {
/// "foo"
/// }
/// }
/// ```
pub WRONG_PUB_SELF_CONVENTION,
restriction,
"defining a public method named with an established prefix (like \"into_\") that takes `self` with the wrong convention"
}

declare_clippy_lint! {
/// **What it does:** Checks for usage of `ok().expect(..)`.
///
Expand Down Expand Up @@ -1658,13 +1634,17 @@ declare_clippy_lint! {
}

pub struct Methods {
avoid_breaking_exported_api: bool,
msrv: Option<RustcVersion>,
}

impl Methods {
#[must_use]
pub fn new(msrv: Option<RustcVersion>) -> Self {
Self { msrv }
pub fn new(avoid_breaking_exported_api: bool, msrv: Option<RustcVersion>) -> Self {
Self {
avoid_breaking_exported_api,
msrv,
}
}
}

Expand All @@ -1673,7 +1653,6 @@ impl_lint_pass!(Methods => [
EXPECT_USED,
SHOULD_IMPLEMENT_TRAIT,
WRONG_SELF_CONVENTION,
WRONG_PUB_SELF_CONVENTION,
OK_EXPECT,
MAP_UNWRAP_OR,
RESULT_MAP_OR_INTO_OPTION,
Expand Down Expand Up @@ -1838,11 +1817,13 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
}
}

if sig.decl.implicit_self.has_implicit_self() {
if sig.decl.implicit_self.has_implicit_self()
&& !(self.avoid_breaking_exported_api
&& cx.access_levels.is_exported(impl_item.hir_id()))
{
wrong_self_convention::check(
cx,
&name,
item.vis.node.is_pub(),
self_ty,
first_arg_ty,
first_arg.pat.span,
Expand Down Expand Up @@ -1915,7 +1896,6 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
wrong_self_convention::check(
cx,
&item.ident.name.as_str(),
false,
self_ty,
first_arg_ty,
first_arg_span,
Expand Down
13 changes: 3 additions & 10 deletions clippy_lints/src/methods/wrong_self_convention.rs
Expand Up @@ -6,7 +6,6 @@ use rustc_middle::ty::TyS;
use rustc_span::source_map::Span;
use std::fmt;

use super::WRONG_PUB_SELF_CONVENTION;
use super::WRONG_SELF_CONVENTION;

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

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

Expand Down Expand Up @@ -85,18 +84,12 @@ impl fmt::Display for Convention {
pub(super) fn check<'tcx>(
cx: &LateContext<'tcx>,
item_name: &str,
is_pub: bool,
self_ty: &'tcx TyS<'tcx>,
first_arg_ty: &'tcx TyS<'tcx>,
first_arg_span: Span,
implements_trait: bool,
is_trait_item: bool,
) {
let lint = if is_pub {
WRONG_PUB_SELF_CONVENTION
} else {
WRONG_SELF_CONVENTION
};
if let Some((conventions, self_kinds)) = &CONVENTIONS.iter().find(|(convs, _)| {
convs
.iter()
Expand Down Expand Up @@ -142,7 +135,7 @@ pub(super) fn check<'tcx>(

span_lint_and_help(
cx,
lint,
WRONG_SELF_CONVENTION,
first_arg_span,
&format!(
"{} usually take {}",
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/def_id_nocore.rs
Expand Up @@ -20,7 +20,7 @@ fn start(_argc: isize, _argv: *const *const u8) -> isize {
0
}

pub struct A;
struct A;

impl A {
pub fn as_ref(self) -> &'static str {
Expand Down
8 changes: 0 additions & 8 deletions tests/ui/module_name_repetitions.rs
Expand Up @@ -15,12 +15,4 @@ mod foo {
pub struct Foobar;
}

#[cfg(test)]
mod test {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}

fn main() {}
1 change: 0 additions & 1 deletion tests/ui/wrong_self_convention.rs
@@ -1,6 +1,5 @@
// edition:2018
#![warn(clippy::wrong_self_convention)]
#![warn(clippy::wrong_pub_self_convention)]
#![allow(dead_code)]

fn main() {}
Expand Down

0 comments on commit d7f47f2

Please sign in to comment.