Skip to content

Commit b5d809a

Browse files
committed
move wrong_self_convention to its own module
1 parent 5912ca9 commit b5d809a

File tree

2 files changed

+86
-71
lines changed

2 files changed

+86
-71
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 10 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ mod uninit_assumed_init;
2525
mod unnecessary_filter_map;
2626
mod unnecessary_lazy_eval;
2727
mod unwrap_used;
28+
mod wrong_self_convention;
2829
mod zst_offset;
2930

3031
use std::borrow::Cow;
31-
use std::fmt;
3232
use std::iter;
3333

3434
use bind_instead_of_map::BindInsteadOfMap;
@@ -1868,7 +1868,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
18681868
}
18691869
}
18701870

1871-
lint_wrong_self_convention(
1871+
wrong_self_convention::check(
18721872
cx,
18731873
&name,
18741874
item.vis.node.is_pub(),
@@ -1924,7 +1924,14 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
19241924
let self_ty = TraitRef::identity(cx.tcx, item.def_id.to_def_id()).self_ty();
19251925

19261926
then {
1927-
lint_wrong_self_convention(cx, &item.ident.name.as_str(), false, self_ty, first_arg_ty, first_arg_span);
1927+
wrong_self_convention::check(
1928+
cx,
1929+
&item.ident.name.as_str(),
1930+
false,
1931+
self_ty,
1932+
first_arg_ty,
1933+
first_arg_span
1934+
);
19281935
}
19291936
}
19301937

@@ -1949,39 +1956,6 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
19491956
extract_msrv_attr!(LateContext);
19501957
}
19511958

1952-
fn lint_wrong_self_convention<'tcx>(
1953-
cx: &LateContext<'tcx>,
1954-
item_name: &str,
1955-
is_pub: bool,
1956-
self_ty: &'tcx TyS<'tcx>,
1957-
first_arg_ty: &'tcx TyS<'tcx>,
1958-
first_arg_span: Span,
1959-
) {
1960-
let lint = if is_pub {
1961-
WRONG_PUB_SELF_CONVENTION
1962-
} else {
1963-
WRONG_SELF_CONVENTION
1964-
};
1965-
if let Some((ref conv, self_kinds)) = &CONVENTIONS.iter().find(|(ref conv, _)| conv.check(item_name)) {
1966-
if !self_kinds.iter().any(|k| k.matches(cx, self_ty, first_arg_ty)) {
1967-
span_lint(
1968-
cx,
1969-
lint,
1970-
first_arg_span,
1971-
&format!(
1972-
"methods called `{}` usually take {}; consider choosing a less ambiguous name",
1973-
conv,
1974-
&self_kinds
1975-
.iter()
1976-
.map(|k| k.description())
1977-
.collect::<Vec<_>>()
1978-
.join(" or ")
1979-
),
1980-
);
1981-
}
1982-
}
1983-
}
1984-
19851959
/// Checks for the `OR_FUN_CALL` lint.
19861960
#[allow(clippy::too_many_lines)]
19871961
fn lint_or_fun_call<'tcx>(
@@ -3415,22 +3389,6 @@ fn lint_into_iter(cx: &LateContext<'_>, expr: &hir::Expr<'_>, self_ref_ty: Ty<'_
34153389
}
34163390
}
34173391

3418-
enum Convention {
3419-
Eq(&'static str),
3420-
StartsWith(&'static str),
3421-
}
3422-
3423-
#[rustfmt::skip]
3424-
const CONVENTIONS: [(Convention, &[SelfKind]); 7] = [
3425-
(Convention::Eq("new"), &[SelfKind::No]),
3426-
(Convention::StartsWith("as_"), &[SelfKind::Ref, SelfKind::RefMut]),
3427-
(Convention::StartsWith("from_"), &[SelfKind::No]),
3428-
(Convention::StartsWith("into_"), &[SelfKind::Value]),
3429-
(Convention::StartsWith("is_"), &[SelfKind::Ref, SelfKind::No]),
3430-
(Convention::Eq("to_mut"), &[SelfKind::RefMut]),
3431-
(Convention::StartsWith("to_"), &[SelfKind::Ref]),
3432-
];
3433-
34343392
const FN_HEADER: hir::FnHeader = hir::FnHeader {
34353393
unsafety: hir::Unsafety::Normal,
34363394
constness: hir::Constness::NotConst,
@@ -3602,25 +3560,6 @@ impl SelfKind {
36023560
}
36033561
}
36043562

3605-
impl Convention {
3606-
#[must_use]
3607-
fn check(&self, other: &str) -> bool {
3608-
match *self {
3609-
Self::Eq(this) => this == other,
3610-
Self::StartsWith(this) => other.starts_with(this) && this != other,
3611-
}
3612-
}
3613-
}
3614-
3615-
impl fmt::Display for Convention {
3616-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
3617-
match *self {
3618-
Self::Eq(this) => this.fmt(f),
3619-
Self::StartsWith(this) => this.fmt(f).and_then(|_| '*'.fmt(f)),
3620-
}
3621-
}
3622-
}
3623-
36243563
#[derive(Clone, Copy)]
36253564
enum OutType {
36263565
Unit,
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use crate::methods::SelfKind;
2+
use crate::utils::span_lint;
3+
use rustc_lint::LateContext;
4+
use rustc_middle::ty::TyS;
5+
use rustc_span::source_map::Span;
6+
use std::fmt;
7+
8+
use super::WRONG_PUB_SELF_CONVENTION;
9+
use super::WRONG_SELF_CONVENTION;
10+
11+
#[rustfmt::skip]
12+
const CONVENTIONS: [(Convention, &[SelfKind]); 7] = [
13+
(Convention::Eq("new"), &[SelfKind::No]),
14+
(Convention::StartsWith("as_"), &[SelfKind::Ref, SelfKind::RefMut]),
15+
(Convention::StartsWith("from_"), &[SelfKind::No]),
16+
(Convention::StartsWith("into_"), &[SelfKind::Value]),
17+
(Convention::StartsWith("is_"), &[SelfKind::Ref, SelfKind::No]),
18+
(Convention::Eq("to_mut"), &[SelfKind::RefMut]),
19+
(Convention::StartsWith("to_"), &[SelfKind::Ref]),
20+
];
21+
enum Convention {
22+
Eq(&'static str),
23+
StartsWith(&'static str),
24+
}
25+
26+
impl Convention {
27+
#[must_use]
28+
fn check(&self, other: &str) -> bool {
29+
match *self {
30+
Self::Eq(this) => this == other,
31+
Self::StartsWith(this) => other.starts_with(this) && this != other,
32+
}
33+
}
34+
}
35+
36+
impl fmt::Display for Convention {
37+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
38+
match *self {
39+
Self::Eq(this) => this.fmt(f),
40+
Self::StartsWith(this) => this.fmt(f).and_then(|_| '*'.fmt(f)),
41+
}
42+
}
43+
}
44+
45+
pub(super) fn check<'tcx>(
46+
cx: &LateContext<'tcx>,
47+
item_name: &str,
48+
is_pub: bool,
49+
self_ty: &'tcx TyS<'tcx>,
50+
first_arg_ty: &'tcx TyS<'tcx>,
51+
first_arg_span: Span,
52+
) {
53+
let lint = if is_pub {
54+
WRONG_PUB_SELF_CONVENTION
55+
} else {
56+
WRONG_SELF_CONVENTION
57+
};
58+
if let Some((ref conv, self_kinds)) = &CONVENTIONS.iter().find(|(ref conv, _)| conv.check(item_name)) {
59+
if !self_kinds.iter().any(|k| k.matches(cx, self_ty, first_arg_ty)) {
60+
span_lint(
61+
cx,
62+
lint,
63+
first_arg_span,
64+
&format!(
65+
"methods called `{}` usually take {}; consider choosing a less ambiguous name",
66+
conv,
67+
&self_kinds
68+
.iter()
69+
.map(|k| k.description())
70+
.collect::<Vec<_>>()
71+
.join(" or ")
72+
),
73+
);
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)