1 use crate::utils::span_lint_and_help;
2 use if_chain::if_chain;
3 use rustc_ast::ast::{Param, TyKind};
4 use rustc_lint::{EarlyContext, EarlyLintPass};
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
8 /// **What it does:** The lint checks for `self` fn fn parameters that explicitly
9 /// specify the `Self`-type explicitly
10 /// **Why is this bad?** Increases the amount and decreases the readability of code
12 /// **Known problems:** None
17 /// pub fn bytes(self: Self) -> usize {
19 /// Self::I32 | Self::F32 => 4,
20 /// Self::I64 | Self::F64 => 8,
26 /// Could be rewritten as
30 /// pub fn bytes(self) -> usize {
32 /// Self::I32 | Self::F32 => 4,
33 /// Self::I64 | Self::F64 => 8,
38 pub NEEDLESS_FN_SELF_TYPE,
40 "type of `self` parameter is already by default `Self`"
43 declare_lint_pass!(NeedlessFnSelfType => [NEEDLESS_FN_SELF_TYPE]);
45 impl EarlyLintPass for NeedlessFnSelfType {
46 fn check_param(&mut self, cx: &EarlyContext<'_>, p: &Param) {
49 if let TyKind::Path(None, path) = &p.ty.kind;
50 if let Some(segment) = path.segments.first();
51 if segment.ident.as_str() == sym!(Self).as_str();
55 NEEDLESS_FN_SELF_TYPE,
57 "the type of the `self` parameter is already by default `Self`",
59 "consider removing the type specification",