Skip to content

Commit 5046126

Browse files
committed
Merge logic of looking for Self type
1 parent 3cb75c2 commit 5046126

File tree

2 files changed

+26
-47
lines changed

2 files changed

+26
-47
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,20 @@ use rustc_ast::ast;
1515
use rustc_errors::Applicability;
1616
use rustc_hir as hir;
1717
use rustc_hir::intravisit::{self, Visitor};
18-
use rustc_hir::{FnRetTy, FnSig, TraitItem, TraitItemKind};
18+
use rustc_hir::{TraitItem, TraitItemKind};
1919
use rustc_lint::{LateContext, LateLintPass, Lint, LintContext};
2020
use rustc_middle::hir::map::Map;
2121
use rustc_middle::lint::in_external_macro;
22-
use rustc_middle::ty::subst::GenericArgKind;
23-
use rustc_middle::ty::{self, Ty, TyS};
22+
use rustc_middle::ty::{self, TraitRef, Ty, TyS};
2423
use rustc_session::{declare_lint_pass, declare_tool_lint};
2524
use rustc_span::source_map::Span;
2625
use rustc_span::symbol::{sym, SymbolStr};
2726

2827
use crate::consts::{constant, Constant};
2928
use crate::utils::usage::mutated_variables;
3029
use crate::utils::{
31-
get_arg_name, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait, in_macro, is_copy,
32-
is_ctor_or_promotable_const_function, is_expn_of, is_self_ty, is_type_diagnostic_item, iter_input_pats,
30+
contains_ty, get_arg_name, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait, in_macro,
31+
is_copy, is_ctor_or_promotable_const_function, is_expn_of, is_type_diagnostic_item, iter_input_pats,
3332
last_path_segment, match_def_path, match_qpath, match_trait_method, match_type, match_var, method_calls,
3433
method_chain_args, paths, remove_blocks, return_ty, single_segment_path, snippet, snippet_with_applicability,
3534
snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_note, span_lint_and_sugg,
@@ -1656,16 +1655,8 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
16561655
if let hir::ImplItemKind::Fn(_, _) = impl_item.kind {
16571656
let ret_ty = return_ty(cx, impl_item.hir_id);
16581657

1659-
let contains_self_ty = |ty: Ty<'tcx>| {
1660-
ty.walk().any(|inner| match inner.unpack() {
1661-
GenericArgKind::Type(inner_ty) => TyS::same_type(self_ty, inner_ty),
1662-
1663-
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
1664-
})
1665-
};
1666-
16671658
// walk the return type and check for Self (this does not check associated types)
1668-
if contains_self_ty(ret_ty) {
1659+
if contains_ty(ret_ty, self_ty) {
16691660
return;
16701661
}
16711662

@@ -1675,7 +1666,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
16751666
for &(predicate, _span) in cx.tcx.predicates_of(def_id).predicates {
16761667
if let ty::PredicateAtom::Projection(projection_predicate) = predicate.skip_binders() {
16771668
// walk the associated type and check for Self
1678-
if contains_self_ty(projection_predicate.ty) {
1669+
if contains_ty(projection_predicate.ty, self_ty) {
16791670
return;
16801671
}
16811672
}
@@ -1696,44 +1687,23 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
16961687
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'_>) {
16971688
if_chain! {
16981689
if item.ident.name == sym!(new);
1699-
if let TraitItemKind::Fn(FnSig { decl, .. }, _) = &item.kind;
1700-
if let FnRetTy::Return(ret_ty) = &decl.output;
1690+
if let TraitItemKind::Fn(_, _) = item.kind;
1691+
let ret_ty = return_ty(cx, item.hir_id);
1692+
let self_ty = TraitRef::identity(cx.tcx, item.hir_id.owner.to_def_id()).self_ty();
1693+
if !contains_ty(ret_ty, self_ty);
17011694

17021695
then {
1703-
let mut visitor = HasSelfVisitor { has_self_ty: false };
1704-
visitor.visit_ty(ret_ty);
1705-
if !visitor.has_self_ty {
1706-
span_lint(
1707-
cx,
1708-
NEW_RET_NO_SELF,
1709-
item.span,
1710-
"methods called `new` usually return `Self`",
1711-
);
1712-
}
1696+
span_lint(
1697+
cx,
1698+
NEW_RET_NO_SELF,
1699+
item.span,
1700+
"methods called `new` usually return `Self`",
1701+
);
17131702
}
17141703
}
17151704
}
17161705
}
17171706

1718-
struct HasSelfVisitor {
1719-
pub has_self_ty: bool,
1720-
}
1721-
1722-
impl<'tcx> intravisit::Visitor<'tcx> for HasSelfVisitor {
1723-
type Map = Map<'tcx>;
1724-
1725-
fn visit_ty(&mut self, ty: &'tcx hir::Ty<'_>) {
1726-
if is_self_ty(ty) {
1727-
self.has_self_ty = true;
1728-
} else {
1729-
intravisit::walk_ty(self, ty);
1730-
}
1731-
}
1732-
fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
1733-
intravisit::NestedVisitorMap::None
1734-
}
1735-
}
1736-
17371707
/// Checks for the `OR_FUN_CALL` lint.
17381708
#[allow(clippy::too_many_lines)]
17391709
fn lint_or_fun_call<'tcx>(

clippy_lints/src/utils/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ use rustc_hir::{
4242
use rustc_infer::infer::TyCtxtInferExt;
4343
use rustc_lint::{LateContext, Level, Lint, LintContext};
4444
use rustc_middle::hir::map::Map;
45-
use rustc_middle::ty::{self, layout::IntegerExt, subst::GenericArg, Ty, TyCtxt, TypeFoldable};
45+
use rustc_middle::ty::subst::{GenericArg, GenericArgKind};
46+
use rustc_middle::ty::{self, layout::IntegerExt, Ty, TyCtxt, TypeFoldable};
4647
use rustc_mir::const_eval;
4748
use rustc_span::hygiene::{ExpnKind, MacroKind};
4849
use rustc_span::source_map::original_sp;
@@ -866,6 +867,14 @@ pub fn return_ty<'tcx>(cx: &LateContext<'tcx>, fn_item: hir::HirId) -> Ty<'tcx>
866867
cx.tcx.erase_late_bound_regions(&ret_ty)
867868
}
868869

870+
/// Walk into `ty` and returns `true` if any inner type is the same as `other_ty`
871+
pub fn contains_ty<'tcx>(ty: Ty<'tcx>, other_ty: Ty<'tcx>) -> bool {
872+
ty.walk().any(|inner| match inner.unpack() {
873+
GenericArgKind::Type(inner_ty) => ty::TyS::same_type(other_ty, inner_ty),
874+
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
875+
})
876+
}
877+
869878
/// Returns `true` if the given type is an `unsafe` function.
870879
pub fn type_is_unsafe_function<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
871880
match ty.kind {

0 commit comments

Comments
 (0)