use rustc_middle::lint::in_external_macro;
use rustc_middle::ty;
use rustc_middle::ty::{DefIdTree, Ty};
-use rustc_session::{declare_lint_pass, declare_tool_lint};
+use rustc_semver::RustcVersion;
+use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::symbol::kw;
use rustc_typeck::hir_ty_to_ty;
-use crate::utils::{differing_macro_contexts, span_lint_and_sugg};
+use crate::utils::{differing_macro_contexts, meets_msrv, span_lint_and_sugg};
declare_clippy_lint! {
/// **What it does:** Checks for unnecessary repetition of structure name when a
/// ```
pub USE_SELF,
nursery,
- "Unnecessary structure name repetition whereas `Self` is applicable"
+ "unnecessary structure name repetition whereas `Self` is applicable"
}
-declare_lint_pass!(UseSelf => [USE_SELF]);
+impl_lint_pass!(UseSelf => [USE_SELF]);
const SEGMENTS_MSG: &str = "segments should be composed of at least 1 element";
-fn span_use_self_lint(cx: &LateContext<'_, '_>, path: &Path<'_>, last_segment: Option<&PathSegment<'_>>) {
+fn span_use_self_lint(cx: &LateContext<'_>, path: &Path<'_>, last_segment: Option<&PathSegment<'_>>) {
let last_segment = last_segment.unwrap_or_else(|| path.segments.last().expect(SEGMENTS_MSG));
// Path segments only include actual path, no methods or fields.
// FIXME: always use this (more correct) visitor, not just in method signatures.
struct SemanticUseSelfVisitor<'a, 'tcx> {
- cx: &'a LateContext<'a, 'tcx>,
+ cx: &'a LateContext<'tcx>,
self_ty: Ty<'tcx>,
}
}
}
-fn check_trait_method_impl_decl<'a, 'tcx>(
- cx: &'a LateContext<'a, 'tcx>,
+fn check_trait_method_impl_decl<'tcx>(
+ cx: &LateContext<'tcx>,
impl_item: &ImplItem<'_>,
impl_decl: &'tcx FnDecl<'_>,
impl_trait_ref: ty::TraitRef<'tcx>,
let trait_method = cx
.tcx
.associated_items(impl_trait_ref.def_id)
- .find_by_name_and_kind(cx.tcx, impl_item.ident, ty::AssocKind::Method, impl_trait_ref.def_id)
+ .find_by_name_and_kind(cx.tcx, impl_item.ident, ty::AssocKind::Fn, impl_trait_ref.def_id)
.expect("impl method matches a trait method");
let trait_method_sig = cx.tcx.fn_sig(trait_method.def_id);
- let trait_method_sig = cx.tcx.erase_late_bound_regions(&trait_method_sig);
+ let trait_method_sig = cx.tcx.erase_late_bound_regions(trait_method_sig);
let output_hir_ty = if let FnRetTy::Return(ty) = &impl_decl.output {
Some(&**ty)
}
}
-impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf {
- fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item<'_>) {
+const USE_SELF_MSRV: RustcVersion = RustcVersion::new(1, 37, 0);
+
+pub struct UseSelf {
+ msrv: Option<RustcVersion>,
+}
+
+impl UseSelf {
+ #[must_use]
+ pub fn new(msrv: Option<RustcVersion>) -> Self {
+ Self { msrv }
+ }
+}
+
+impl<'tcx> LateLintPass<'tcx> for UseSelf {
+ fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
+ if !meets_msrv(self.msrv.as_ref(), &USE_SELF_MSRV) {
+ return;
+ }
+
if in_external_macro(cx.sess(), item.span) {
return;
}
if let TyKind::Path(QPath::Resolved(_, ref item_path)) = item_type.kind;
then {
let parameters = &item_path.segments.last().expect(SEGMENTS_MSG).args;
- let should_check = if let Some(ref params) = *parameters {
- !params.parenthesized && !params.args.iter().any(|arg| match arg {
- GenericArg::Lifetime(_) => true,
- _ => false,
- })
- } else {
- true
- };
+ let should_check = parameters.as_ref().map_or(
+ true,
+ |params| !params.parenthesized
+ &&!params.args.iter().any(|arg| matches!(arg, GenericArg::Lifetime(_)))
+ );
if should_check {
let visitor = &mut UseSelfVisitor {
}
}
}
+ extract_msrv_attr!(LateContext);
}
struct UseSelfVisitor<'a, 'tcx> {
item_path: &'a Path<'a>,
- cx: &'a LateContext<'a, 'tcx>,
+ cx: &'a LateContext<'tcx>,
}
impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {