Skip to content

Commit 69ef96b

Browse files
committed
adopt comments from review
1 parent 52f7452 commit 69ef96b

9 files changed

+231
-128
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1616,13 +1616,13 @@ Released 2018-09-13
16161616
[`mutex_atomic`]: https://rust-lang.github.io/rust-clippy/master/index.html#mutex_atomic
16171617
[`mutex_integer`]: https://rust-lang.github.io/rust-clippy/master/index.html#mutex_integer
16181618
[`naive_bytecount`]: https://rust-lang.github.io/rust-clippy/master/index.html#naive_bytecount
1619+
[`needless_arbitrary_self_type`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_arbitrary_self_type
16191620
[`needless_bool`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_bool
16201621
[`needless_borrow`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
16211622
[`needless_borrowed_reference`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrowed_reference
16221623
[`needless_collect`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_collect
16231624
[`needless_continue`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_continue
16241625
[`needless_doctest_main`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_doctest_main
1625-
[`needless_fn_self_type`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_fn_self_type
16261626
[`needless_lifetimes`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes
16271627
[`needless_pass_by_value`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_value
16281628
[`needless_range_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_range_loop

clippy_lints/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,11 @@ mod mut_mut;
250250
mod mut_reference;
251251
mod mutable_debug_assertion;
252252
mod mutex_atomic;
253+
mod needless_arbitrary_self_type;
253254
mod needless_bool;
254255
mod needless_borrow;
255256
mod needless_borrowed_ref;
256257
mod needless_continue;
257-
mod needless_fn_self_type;
258258
mod needless_pass_by_value;
259259
mod needless_update;
260260
mod neg_cmp_op_on_partial_ord;
@@ -717,12 +717,12 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
717717
&mutable_debug_assertion::DEBUG_ASSERT_WITH_MUT_CALL,
718718
&mutex_atomic::MUTEX_ATOMIC,
719719
&mutex_atomic::MUTEX_INTEGER,
720+
&needless_arbitrary_self_type::NEEDLESS_ARBITRARY_SELF_TYPE,
720721
&needless_bool::BOOL_COMPARISON,
721722
&needless_bool::NEEDLESS_BOOL,
722723
&needless_borrow::NEEDLESS_BORROW,
723724
&needless_borrowed_ref::NEEDLESS_BORROWED_REFERENCE,
724725
&needless_continue::NEEDLESS_CONTINUE,
725-
&needless_fn_self_type::NEEDLESS_FN_SELF_TYPE,
726726
&needless_pass_by_value::NEEDLESS_PASS_BY_VALUE,
727727
&needless_update::NEEDLESS_UPDATE,
728728
&neg_cmp_op_on_partial_ord::NEG_CMP_OP_ON_PARTIAL_ORD,
@@ -1027,7 +1027,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10271027
store.register_early_pass(|| box items_after_statements::ItemsAfterStatements);
10281028
store.register_early_pass(|| box precedence::Precedence);
10291029
store.register_early_pass(|| box needless_continue::NeedlessContinue);
1030-
store.register_early_pass(|| box needless_fn_self_type::NeedlessFnSelfType);
1030+
store.register_early_pass(|| box needless_arbitrary_self_type::NeedlessArbitrarySelfType);
10311031
store.register_early_pass(|| box redundant_static_lifetimes::RedundantStaticLifetimes);
10321032
store.register_late_pass(|| box cargo_common_metadata::CargoCommonMetadata);
10331033
store.register_late_pass(|| box multiple_crate_versions::MultipleCrateVersions);
@@ -1371,10 +1371,10 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
13711371
LintId::of(&mut_key::MUTABLE_KEY_TYPE),
13721372
LintId::of(&mut_reference::UNNECESSARY_MUT_PASSED),
13731373
LintId::of(&mutex_atomic::MUTEX_ATOMIC),
1374+
LintId::of(&needless_arbitrary_self_type::NEEDLESS_ARBITRARY_SELF_TYPE),
13741375
LintId::of(&needless_bool::BOOL_COMPARISON),
13751376
LintId::of(&needless_bool::NEEDLESS_BOOL),
13761377
LintId::of(&needless_borrowed_ref::NEEDLESS_BORROWED_REFERENCE),
1377-
LintId::of(&needless_fn_self_type::NEEDLESS_FN_SELF_TYPE),
13781378
LintId::of(&needless_update::NEEDLESS_UPDATE),
13791379
LintId::of(&neg_cmp_op_on_partial_ord::NEG_CMP_OP_ON_PARTIAL_ORD),
13801380
LintId::of(&neg_multiply::NEG_MULTIPLY),
@@ -1534,7 +1534,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
15341534
LintId::of(&misc_early::MIXED_CASE_HEX_LITERALS),
15351535
LintId::of(&misc_early::REDUNDANT_PATTERN),
15361536
LintId::of(&mut_reference::UNNECESSARY_MUT_PASSED),
1537-
LintId::of(&needless_fn_self_type::NEEDLESS_FN_SELF_TYPE),
15381537
LintId::of(&neg_multiply::NEG_MULTIPLY),
15391538
LintId::of(&new_without_default::NEW_WITHOUT_DEFAULT),
15401539
LintId::of(&non_expressive_names::JUST_UNDERSCORES_AND_DIGITS),
@@ -1603,6 +1602,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
16031602
LintId::of(&misc::SHORT_CIRCUIT_STATEMENT),
16041603
LintId::of(&misc_early::UNNEEDED_WILDCARD_PATTERN),
16051604
LintId::of(&misc_early::ZERO_PREFIXED_LITERAL),
1605+
LintId::of(&needless_arbitrary_self_type::NEEDLESS_ARBITRARY_SELF_TYPE),
16061606
LintId::of(&needless_bool::BOOL_COMPARISON),
16071607
LintId::of(&needless_bool::NEEDLESS_BOOL),
16081608
LintId::of(&needless_borrowed_ref::NEEDLESS_BORROWED_REFERENCE),
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
use crate::utils::span_lint_and_sugg;
2+
use if_chain::if_chain;
3+
use rustc_ast::ast::{BindingMode, Lifetime, Mutability, Param, PatKind, Path, TyKind};
4+
use rustc_errors::Applicability;
5+
use rustc_lint::{EarlyContext, EarlyLintPass};
6+
use rustc_session::{declare_lint_pass, declare_tool_lint};
7+
use rustc_span::symbol::kw;
8+
use rustc_span::Span;
9+
10+
declare_clippy_lint! {
11+
/// **What it does:** The lint checks for `self` fn fn parameters that
12+
/// specify the `Self`-type explicitly
13+
/// **Why is this bad?** Increases the amount and decreases the readability of code
14+
///
15+
/// **Known problems:** None
16+
///
17+
/// **Example:**
18+
/// ```rust
19+
/// enum ValType {
20+
/// I32,
21+
/// I64,
22+
/// F32,
23+
/// F64,
24+
/// }
25+
///
26+
/// impl ValType {
27+
/// pub fn bytes(self: Self) -> usize {
28+
/// match self {
29+
/// Self::I32 | Self::F32 => 4,
30+
/// Self::I64 | Self::F64 => 8,
31+
/// }
32+
/// }
33+
/// }
34+
/// ```
35+
///
36+
/// Could be rewritten as
37+
///
38+
/// ```rust
39+
/// enum ValType {
40+
/// I32,
41+
/// I64,
42+
/// F32,
43+
/// F64,
44+
/// }
45+
///
46+
/// impl ValType {
47+
/// pub fn bytes(self) -> usize {
48+
/// match self {
49+
/// Self::I32 | Self::F32 => 4,
50+
/// Self::I64 | Self::F64 => 8,
51+
/// }
52+
/// }
53+
/// }
54+
/// ```
55+
pub NEEDLESS_ARBITRARY_SELF_TYPE,
56+
complexity,
57+
"type of `self` parameter is already by default `Self`"
58+
}
59+
60+
declare_lint_pass!(NeedlessArbitrarySelfType => [NEEDLESS_ARBITRARY_SELF_TYPE]);
61+
62+
enum Mode {
63+
Ref(Option<Lifetime>),
64+
Value,
65+
}
66+
67+
fn check_param_inner(cx: &EarlyContext<'_>, path: &Path, span: Span, binding_mode: &Mode, mutbl: Mutability) {
68+
if_chain! {
69+
if let [segment] = &path.segments[..];
70+
if segment.ident.name == kw::SelfUpper;
71+
then {
72+
let self_param = match (binding_mode, mutbl) {
73+
(Mode::Ref(None), Mutability::Mut) => "&mut self".to_string(),
74+
(Mode::Ref(Some(lifetime)), Mutability::Mut) => format!("&{} mut self", &lifetime.ident.name),
75+
(Mode::Ref(None), Mutability::Not) => "&self".to_string(),
76+
(Mode::Ref(Some(lifetime)), Mutability::Not) => format!("&{} self", &lifetime.ident.name),
77+
(Mode::Value, Mutability::Mut) => "mut self".to_string(),
78+
(Mode::Value, Mutability::Not) => "self".to_string(),
79+
};
80+
81+
span_lint_and_sugg(
82+
cx,
83+
NEEDLESS_ARBITRARY_SELF_TYPE,
84+
span,
85+
"the type of the `self` parameter is arbitrary",
86+
"consider to change this parameter to",
87+
self_param,
88+
Applicability::MachineApplicable,
89+
)
90+
}
91+
}
92+
}
93+
94+
impl EarlyLintPass for NeedlessArbitrarySelfType {
95+
fn check_param(&mut self, cx: &EarlyContext<'_>, p: &Param) {
96+
if !p.is_self() {
97+
return;
98+
}
99+
100+
match &p.ty.kind {
101+
TyKind::Path(None, path) => {
102+
if let PatKind::Ident(BindingMode::ByValue(mutbl), _, _) = p.pat.kind {
103+
check_param_inner(cx, path, p.span.to(p.ty.span), &Mode::Value, mutbl)
104+
}
105+
},
106+
TyKind::Rptr(lifetime, mut_ty) => {
107+
if let TyKind::Path(None, path) = &mut_ty.ty.kind {
108+
check_param_inner(cx, path, p.span.to(p.ty.span), &Mode::Ref(*lifetime), mut_ty.mutbl)
109+
}
110+
},
111+
_ => {},
112+
}
113+
}
114+
}

clippy_lints/src/needless_fn_self_type.rs

Lines changed: 0 additions & 78 deletions
This file was deleted.

src/lintlist/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,13 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
14591459
deprecation: None,
14601460
module: "bytecount",
14611461
},
1462+
Lint {
1463+
name: "needless_arbitrary_self_type",
1464+
group: "complexity",
1465+
desc: "type of `self` parameter is already by default `Self`",
1466+
deprecation: None,
1467+
module: "needless_arbitrary_self_type",
1468+
},
14621469
Lint {
14631470
name: "needless_bool",
14641471
group: "complexity",
@@ -1501,13 +1508,6 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
15011508
deprecation: None,
15021509
module: "doc",
15031510
},
1504-
Lint {
1505-
name: "needless_fn_self_type",
1506-
group: "style",
1507-
desc: "type of `self` parameter is already by default `Self`",
1508-
deprecation: None,
1509-
module: "needless_fn_self_type",
1510-
},
15111511
Lint {
15121512
name: "needless_lifetimes",
15131513
group: "complexity",
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#![warn(clippy::needless_arbitrary_self_type)]
2+
3+
pub enum ValType {
4+
A,
5+
B,
6+
}
7+
8+
impl ValType {
9+
pub fn bad(self: Self) {
10+
unimplemented!();
11+
}
12+
13+
pub fn good(self) {
14+
unimplemented!();
15+
}
16+
17+
pub fn mut_bad(mut self: Self) {
18+
unimplemented!();
19+
}
20+
21+
pub fn mut_good(mut self) {
22+
unimplemented!();
23+
}
24+
25+
pub fn ref_bad(self: &Self) {
26+
unimplemented!();
27+
}
28+
29+
pub fn ref_bad_with_lifetime<'a>(self: &'a Self) {
30+
unimplemented!();
31+
}
32+
33+
pub fn ref_good(&self) {
34+
unimplemented!();
35+
}
36+
37+
pub fn mut_ref_bad(self: &mut Self) {
38+
unimplemented!();
39+
}
40+
41+
pub fn mut_ref_bad_with_lifetime<'a>(self: &'a mut Self) {
42+
unimplemented!();
43+
}
44+
45+
pub fn mut_ref_good(&mut self) {
46+
unimplemented!();
47+
}
48+
49+
pub fn mut_ref_mut_bad(mut self: &mut Self) {
50+
unimplemented!();
51+
}
52+
53+
pub fn mut_ref_mut_ref_good(self: &&mut &mut Self) {
54+
unimplemented!();
55+
}
56+
}
57+
58+
fn main() {}

0 commit comments

Comments
 (0)