[`needless_collect`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_collect
[`needless_continue`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_continue
[`needless_doctest_main`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_doctest_main
+[`needless_fn_self_type`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_fn_self_type
[`needless_lifetimes`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes
[`needless_pass_by_value`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_value
[`needless_range_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_range_loop
mod needless_borrow;
mod needless_borrowed_ref;
mod needless_continue;
+mod needless_fn_self_type;
mod needless_pass_by_value;
mod needless_update;
mod neg_cmp_op_on_partial_ord;
&needless_borrow::NEEDLESS_BORROW,
&needless_borrowed_ref::NEEDLESS_BORROWED_REFERENCE,
&needless_continue::NEEDLESS_CONTINUE,
+ &needless_fn_self_type::NEEDLESS_FN_SELF_TYPE,
&needless_pass_by_value::NEEDLESS_PASS_BY_VALUE,
&needless_update::NEEDLESS_UPDATE,
&neg_cmp_op_on_partial_ord::NEG_CMP_OP_ON_PARTIAL_ORD,
store.register_early_pass(|| box items_after_statements::ItemsAfterStatements);
store.register_early_pass(|| box precedence::Precedence);
store.register_early_pass(|| box needless_continue::NeedlessContinue);
+ store.register_early_pass(|| box needless_fn_self_type::NeedlessFnSelfType);
store.register_early_pass(|| box redundant_static_lifetimes::RedundantStaticLifetimes);
store.register_late_pass(|| box cargo_common_metadata::CargoCommonMetadata);
store.register_late_pass(|| box multiple_crate_versions::MultipleCrateVersions);
LintId::of(&needless_bool::BOOL_COMPARISON),
LintId::of(&needless_bool::NEEDLESS_BOOL),
LintId::of(&needless_borrowed_ref::NEEDLESS_BORROWED_REFERENCE),
+ LintId::of(&needless_fn_self_type::NEEDLESS_FN_SELF_TYPE),
LintId::of(&needless_update::NEEDLESS_UPDATE),
LintId::of(&neg_cmp_op_on_partial_ord::NEG_CMP_OP_ON_PARTIAL_ORD),
LintId::of(&neg_multiply::NEG_MULTIPLY),
LintId::of(&misc_early::MIXED_CASE_HEX_LITERALS),
LintId::of(&misc_early::REDUNDANT_PATTERN),
LintId::of(&mut_reference::UNNECESSARY_MUT_PASSED),
+ LintId::of(&needless_fn_self_type::NEEDLESS_FN_SELF_TYPE),
LintId::of(&neg_multiply::NEG_MULTIPLY),
LintId::of(&new_without_default::NEW_WITHOUT_DEFAULT),
LintId::of(&non_expressive_names::JUST_UNDERSCORES_AND_DIGITS),
--- /dev/null
+use crate::utils::span_lint_and_help;
+use if_chain::if_chain;
+use rustc_ast::ast::{Param, TyKind};
+use rustc_lint::{EarlyContext, EarlyLintPass};
+use rustc_session::{declare_lint_pass, declare_tool_lint};
+
+declare_clippy_lint! {
+ /// **What it does:** The lint checks for `self` fn fn parameters that explicitly
+ /// specify the `Self`-type explicitly
+ /// **Why is this bad?** Increases the amount and decreases the readability of code
+ ///
+ /// **Known problems:** None
+ ///
+ /// **Example:**
+ /// ```rust
+ /// impl ValType {
+ /// pub fn bytes(self: Self) -> usize {
+ /// match self {
+ /// Self::I32 | Self::F32 => 4,
+ /// Self::I64 | Self::F64 => 8,
+ /// }
+ /// }
+ /// }
+ /// ```
+ ///
+ /// Could be rewritten as
+ ///
+ /// ```rust
+ /// impl ValType {
+ /// pub fn bytes(self) -> usize {
+ /// match self {
+ /// Self::I32 | Self::F32 => 4,
+ /// Self::I64 | Self::F64 => 8,
+ /// }
+ /// }
+ /// }
+ /// ```
+ pub NEEDLESS_FN_SELF_TYPE,
+ style,
+ "type of `self` parameter is already by default `Self`"
+}
+
+declare_lint_pass!(NeedlessFnSelfType => [NEEDLESS_FN_SELF_TYPE]);
+
+impl EarlyLintPass for NeedlessFnSelfType {
+ fn check_param(&mut self, cx: &EarlyContext<'_>, p: &Param) {
+ if_chain! {
+ if p.is_self();
+ if let TyKind::Path(None, path) = &p.ty.kind;
+ if let Some(segment) = path.segments.first();
+ if segment.ident.as_str() == sym!(Self).as_str();
+ then {
+ span_lint_and_help(
+ cx,
+ NEEDLESS_FN_SELF_TYPE,
+ p.ty.span,
+ "the type of the `self` parameter is already by default `Self`",
+ None,
+ "consider removing the type specification",
+ );
+ }
+ }
+ }
+}
/// fn func<T: Clone + Default>(arg: T) {}
/// ```
/// or
- /// ///
+ ///
/// ```rust
/// fn func<T>(arg: T) where T: Clone + Default {}
/// ```
deprecation: None,
module: "doc",
},
+ Lint {
+ name: "needless_fn_self_type",
+ group: "style",
+ desc: "type of `self` parameter is already by default `Self`",
+ deprecation: None,
+ module: "needless_fn_self_type",
+ },
Lint {
name: "needless_lifetimes",
group: "complexity",
--- /dev/null
+#![warn(clippy::style, clippy::needless_fn_self_type)]
+
+pub enum ValType {
+ I32,
+ I64,
+ F32,
+ F64,
+}
+
+impl ValType {
+ pub fn bytes_bad(self: Self) -> usize {
+ match self {
+ Self::I32 | Self::F32 => 4,
+ Self::I64 | Self::F64 => 8,
+ }
+ }
+
+ pub fn bytes_good(self) -> usize {
+ match self {
+ Self::I32 | Self::F32 => 4,
+ Self::I64 | Self::F64 => 8,
+ }
+ }
+}
+
+fn main() {}
--- /dev/null
+error: the type of the `self` parameter is already by default `Self`
+ --> $DIR/needless_fn_self_type.rs:11:28
+ |
+LL | pub fn bytes_bad(self: Self) -> usize {
+ | ^^^^
+ |
+ = note: `-D clippy::needless-fn-self-type` implied by `-D warnings`
+ = help: consider removing the type specification
+
+error: aborting due to previous error
+