Skip to content

Commit 6d30b1d

Browse files
committed
Fix adjacent code
1 parent 7a22c07 commit 6d30b1d

24 files changed

+99
-106
lines changed

clippy_lints/src/await_holding_invalid.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_hir::{def::Res, AsyncGeneratorKind, Body, BodyId, GeneratorKind};
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_middle::ty::GeneratorInteriorTypeCause;
88
use rustc_session::{declare_tool_lint, impl_lint_pass};
9-
use rustc_span::Span;
9+
use rustc_span::{sym, Span};
1010

1111
use crate::utils::conf::DisallowedType;
1212

@@ -276,9 +276,9 @@ fn emit_invalid_type(cx: &LateContext<'_>, span: Span, disallowed: &DisallowedTy
276276
}
277277

278278
fn is_mutex_guard(cx: &LateContext<'_>, def_id: DefId) -> bool {
279-
match_def_path(cx, def_id, &paths::MUTEX_GUARD)
280-
|| match_def_path(cx, def_id, &paths::RWLOCK_READ_GUARD)
281-
|| match_def_path(cx, def_id, &paths::RWLOCK_WRITE_GUARD)
279+
cx.tcx.is_diagnostic_item(sym::MutexGuard, def_id)
280+
|| cx.tcx.is_diagnostic_item(sym::RwLockReadGuard, def_id)
281+
|| cx.tcx.is_diagnostic_item(sym::RwLockWriteGuard, def_id)
282282
|| match_def_path(cx, def_id, &paths::PARKING_LOT_MUTEX_GUARD)
283283
|| match_def_path(cx, def_id, &paths::PARKING_LOT_RWLOCK_READ_GUARD)
284284
|| match_def_path(cx, def_id, &paths::PARKING_LOT_RWLOCK_WRITE_GUARD)

clippy_lints/src/booleans.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
2+
use clippy_utils::eq_expr_value;
23
use clippy_utils::source::snippet_opt;
34
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
4-
use clippy_utils::{eq_expr_value, get_trait_def_id, paths};
55
use if_chain::if_chain;
66
use rustc_ast::ast::LitKind;
77
use rustc_errors::Applicability;
@@ -483,7 +483,9 @@ impl<'a, 'tcx> Visitor<'tcx> for NonminimalBoolVisitor<'a, 'tcx> {
483483

484484
fn implements_ord<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>) -> bool {
485485
let ty = cx.typeck_results().expr_ty(expr);
486-
get_trait_def_id(cx, &paths::ORD).map_or(false, |id| implements_trait(cx, ty, id, &[]))
486+
cx.tcx
487+
.get_diagnostic_item(sym::Ord)
488+
.map_or(false, |id| implements_trait(cx, ty, id, &[]))
487489
}
488490

489491
struct NotSimplificationVisitor<'a, 'tcx> {

clippy_lints/src/comparison_chain.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::ty::implements_trait;
3-
use clippy_utils::{get_trait_def_id, if_sequence, in_constant, is_else_clause, paths, SpanlessEq};
3+
use clippy_utils::{if_sequence, in_constant, is_else_clause, SpanlessEq};
44
use rustc_hir::{BinOpKind, Expr, ExprKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
7+
use rustc_span::sym;
78

89
declare_clippy_lint! {
910
/// ### What it does
@@ -106,7 +107,10 @@ impl<'tcx> LateLintPass<'tcx> for ComparisonChain {
106107

107108
// Check that the type being compared implements `core::cmp::Ord`
108109
let ty = cx.typeck_results().expr_ty(lhs1);
109-
let is_ord = get_trait_def_id(cx, &paths::ORD).map_or(false, |id| implements_trait(cx, ty, id, &[]));
110+
let is_ord = cx
111+
.tcx
112+
.get_diagnostic_item(sym::Ord)
113+
.map_or(false, |id| implements_trait(cx, ty, id, &[]));
110114

111115
if !is_ord {
112116
return;

clippy_lints/src/functions/must_use.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ use rustc_middle::{
77
lint::in_external_macro,
88
ty::{self, Ty},
99
};
10-
use rustc_span::{sym, Span};
10+
use rustc_span::{sym, Span, Symbol};
1111

1212
use clippy_utils::attrs::is_proc_macro;
1313
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_then};
1414
use clippy_utils::source::snippet_opt;
1515
use clippy_utils::ty::is_must_use_ty;
16-
use clippy_utils::{match_def_path, return_ty, trait_ref_of_method};
16+
use clippy_utils::{return_ty, trait_ref_of_method};
1717

1818
use super::{DOUBLE_MUST_USE, MUST_USE_CANDIDATE, MUST_USE_UNIT};
1919

@@ -178,15 +178,17 @@ fn is_mutable_pat(cx: &LateContext<'_>, pat: &hir::Pat<'_>, tys: &mut DefIdSet)
178178
}
179179
}
180180

181-
static KNOWN_WRAPPER_TYS: &[&[&str]] = &[&["alloc", "rc", "Rc"], &["std", "sync", "Arc"]];
181+
static KNOWN_WRAPPER_TYS: &[Symbol] = &[sym::Rc, sym::Arc];
182182

183183
fn is_mutable_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, span: Span, tys: &mut DefIdSet) -> bool {
184184
match *ty.kind() {
185185
// primitive types are never mutable
186186
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Str => false,
187187
ty::Adt(adt, substs) => {
188188
tys.insert(adt.did()) && !ty.is_freeze(cx.tcx.at(span), cx.param_env)
189-
|| KNOWN_WRAPPER_TYS.iter().any(|path| match_def_path(cx, adt.did(), path))
189+
|| KNOWN_WRAPPER_TYS
190+
.iter()
191+
.any(|&sym| cx.tcx.is_diagnostic_item(sym, adt.did()))
190192
&& substs.types().any(|ty| is_mutable_ty(cx, ty, span, tys))
191193
},
192194
ty::Tuple(substs) => substs.iter().any(|ty| is_mutable_ty(cx, ty, span, tys)),

clippy_lints/src/infinite_iter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint;
22
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
3-
use clippy_utils::{higher, match_def_path, path_def_id, paths};
3+
use clippy_utils::{higher, path_def_id};
44
use rustc_hir::{BorrowKind, Closure, Expr, ExprKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -169,7 +169,7 @@ fn is_infinite(cx: &LateContext<'_>, expr: &Expr<'_>) -> Finiteness {
169169
ExprKind::Block(block, _) => block.expr.as_ref().map_or(Finite, |e| is_infinite(cx, e)),
170170
ExprKind::Box(e) | ExprKind::AddrOf(BorrowKind::Ref, _, e) => is_infinite(cx, e),
171171
ExprKind::Call(path, _) => path_def_id(cx, path)
172-
.map_or(false, |id| match_def_path(cx, id, &paths::ITER_REPEAT))
172+
.map_or(false, |id| cx.tcx.is_diagnostic_item(sym::iter_repeat, id))
173173
.into(),
174174
ExprKind::Struct(..) => higher::Range::hir(expr).map_or(false, |r| r.end.is_none()).into(),
175175
_ => Finite,

clippy_lints/src/inherent_to_string.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
3-
use clippy_utils::{get_trait_def_id, paths, return_ty, trait_ref_of_method};
3+
use clippy_utils::{return_ty, trait_ref_of_method};
44
use if_chain::if_chain;
55
use rustc_hir::{GenericParamKind, ImplItem, ImplItemKind};
66
use rustc_lint::{LateContext, LateLintPass};
@@ -118,7 +118,10 @@ impl<'tcx> LateLintPass<'tcx> for InherentToString {
118118
}
119119

120120
fn show_lint(cx: &LateContext<'_>, item: &ImplItem<'_>) {
121-
let display_trait_id = get_trait_def_id(cx, &paths::DISPLAY_TRAIT).expect("Failed to get trait ID of `Display`!");
121+
let display_trait_id = cx
122+
.tcx
123+
.get_diagnostic_item(sym::Display)
124+
.expect("Failed to get trait ID of `Display`!");
122125

123126
// Get the real type of 'self'
124127
let self_type = cx.tcx.fn_sig(item.def_id).input(0);

clippy_lints/src/let_underscore.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::ty::{is_must_use_ty, match_type};
2+
use clippy_utils::ty::{is_must_use_ty, is_type_diagnostic_item, match_type};
33
use clippy_utils::{is_must_use_func_call, paths};
44
use if_chain::if_chain;
55
use rustc_hir::{Local, PatKind};
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_middle::lint::in_external_macro;
88
use rustc_middle::ty::subst::GenericArgKind;
99
use rustc_session::{declare_lint_pass, declare_tool_lint};
10+
use rustc_span::{sym, Symbol};
1011

1112
declare_clippy_lint! {
1213
/// ### What it does
@@ -99,10 +100,9 @@ declare_clippy_lint! {
99100

100101
declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_MUST_USE, LET_UNDERSCORE_LOCK, LET_UNDERSCORE_DROP]);
101102

102-
const SYNC_GUARD_PATHS: [&[&str]; 6] = [
103-
&paths::MUTEX_GUARD,
104-
&paths::RWLOCK_READ_GUARD,
105-
&paths::RWLOCK_WRITE_GUARD,
103+
const SYNC_GUARD_SYMS: [Symbol; 3] = [sym::MutexGuard, sym::RwLockReadGuard, sym::RwLockWriteGuard];
104+
105+
const SYNC_GUARD_PATHS: [&[&str]; 3] = [
106106
&paths::PARKING_LOT_MUTEX_GUARD,
107107
&paths::PARKING_LOT_RWLOCK_READ_GUARD,
108108
&paths::PARKING_LOT_RWLOCK_WRITE_GUARD,
@@ -121,7 +121,10 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
121121
let init_ty = cx.typeck_results().expr_ty(init);
122122
let contains_sync_guard = init_ty.walk().any(|inner| match inner.unpack() {
123123
GenericArgKind::Type(inner_ty) => {
124-
SYNC_GUARD_PATHS.iter().any(|path| match_type(cx, inner_ty, path))
124+
SYNC_GUARD_SYMS
125+
.iter()
126+
.any(|&sym| is_type_diagnostic_item(cx, inner_ty, sym))
127+
|| SYNC_GUARD_PATHS.iter().any(|path| match_type(cx, inner_ty, path))
125128
},
126129

127130
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
@@ -134,7 +137,7 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
134137
"non-binding let on a synchronization lock",
135138
None,
136139
"consider using an underscore-prefixed named \
137-
binding or dropping explicitly with `std::mem::drop`"
140+
binding or dropping explicitly with `std::mem::drop`",
138141
);
139142
} else if init_ty.needs_drop(cx.tcx, cx.param_env) {
140143
span_lint_and_help(
@@ -144,7 +147,7 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
144147
"non-binding `let` on a type that implements `Drop`",
145148
None,
146149
"consider using an underscore-prefixed named \
147-
binding or dropping explicitly with `std::mem::drop`"
150+
binding or dropping explicitly with `std::mem::drop`",
148151
);
149152
} else if is_must_use_ty(cx, cx.typeck_results().expr_ty(init)) {
150153
span_lint_and_help(
@@ -153,7 +156,7 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
153156
local.span,
154157
"non-binding let on an expression with `#[must_use]` type",
155158
None,
156-
"consider explicitly using expression value"
159+
"consider explicitly using expression value",
157160
);
158161
} else if is_must_use_func_call(cx, init) {
159162
span_lint_and_help(
@@ -162,7 +165,7 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
162165
local.span,
163166
"non-binding let on a result of a `#[must_use]` function",
164167
None,
165-
"consider explicitly using function result"
168+
"consider explicitly using function result",
166169
);
167170
}
168171
}

clippy_lints/src/matches/single_match.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::{expr_block, snippet};
3-
use clippy_utils::ty::{implements_trait, match_type, peel_mid_ty_refs};
4-
use clippy_utils::{
5-
is_lint_allowed, is_unit_expr, is_wild, paths, peel_blocks, peel_hir_pat_refs, peel_n_hir_expr_refs,
6-
};
3+
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item, peel_mid_ty_refs};
4+
use clippy_utils::{is_lint_allowed, is_unit_expr, is_wild, peel_blocks, peel_hir_pat_refs, peel_n_hir_expr_refs};
75
use core::cmp::max;
86
use rustc_errors::Applicability;
97
use rustc_hir::{Arm, BindingAnnotation, Block, Expr, ExprKind, Pat, PatKind};
108
use rustc_lint::LateContext;
119
use rustc_middle::ty::{self, Ty};
10+
use rustc_span::sym;
1211

1312
use super::{MATCH_BOOL, SINGLE_MATCH, SINGLE_MATCH_ELSE};
1413

@@ -156,10 +155,10 @@ fn pat_in_candidate_enum<'a>(cx: &LateContext<'a>, ty: Ty<'a>, pat: &Pat<'_>) ->
156155
/// Returns `true` if the given type is an enum we know won't be expanded in the future
157156
fn in_candidate_enum<'a>(cx: &LateContext<'a>, ty: Ty<'_>) -> bool {
158157
// list of candidate `Enum`s we know will never get any more members
159-
let candidates = [&paths::COW, &paths::OPTION, &paths::RESULT];
158+
let candidates = [sym::Cow, sym::Option, sym::Result];
160159

161160
for candidate_ty in candidates {
162-
if match_type(cx, ty, candidate_ty) {
161+
if is_type_diagnostic_item(cx, ty, candidate_ty) {
163162
return true;
164163
}
165164
}

clippy_lints/src/methods/filetype_is_file.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::ty::match_type;
3-
use clippy_utils::{get_parent_expr, paths};
2+
use clippy_utils::get_parent_expr;
3+
use clippy_utils::ty::is_type_diagnostic_item;
44
use if_chain::if_chain;
55
use rustc_hir as hir;
66
use rustc_lint::LateContext;
7-
use rustc_span::source_map::Span;
7+
use rustc_span::{source_map::Span, sym};
88

99
use super::FILETYPE_IS_FILE;
1010

1111
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>) {
1212
let ty = cx.typeck_results().expr_ty(recv);
1313

14-
if !match_type(cx, ty, &paths::FILE_TYPE) {
14+
if !is_type_diagnostic_item(cx, ty, sym::FileType) {
1515
return;
1616
}
1717

clippy_lints/src/methods/inefficient_to_string.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ fn specializes_tostring(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
6565
}
6666

6767
if let ty::Adt(adt, substs) = ty.kind() {
68-
match_def_path(cx, adt.did(), &paths::COW) && substs.type_at(1).is_str()
68+
cx.tcx.is_diagnostic_item(sym::Cow, adt.did()) && substs.type_at(1).is_str()
6969
} else {
7070
false
7171
}

clippy_lints/src/methods/manual_str_repeat.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::is_path_diagnostic_item;
23
use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
34
use clippy_utils::sugg::Sugg;
4-
use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item, match_type};
5-
use clippy_utils::{is_expr_path_def_path, paths};
5+
use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item};
66
use if_chain::if_chain;
77
use rustc_ast::LitKind;
88
use rustc_errors::Applicability;
@@ -38,7 +38,7 @@ fn parse_repeat_arg(cx: &LateContext<'_>, e: &Expr<'_>) -> Option<RepeatKind> {
3838
let ty = cx.typeck_results().expr_ty(e);
3939
if is_type_diagnostic_item(cx, ty, sym::String)
4040
|| (is_type_lang_item(cx, ty, LangItem::OwnedBox) && get_ty_param(ty).map_or(false, Ty::is_str))
41-
|| (match_type(cx, ty, &paths::COW) && get_ty_param(ty).map_or(false, Ty::is_str))
41+
|| (is_type_diagnostic_item(cx, ty, sym::Cow) && get_ty_param(ty).map_or(false, Ty::is_str))
4242
{
4343
Some(RepeatKind::String)
4444
} else {
@@ -57,7 +57,7 @@ pub(super) fn check(
5757
) {
5858
if_chain! {
5959
if let ExprKind::Call(repeat_fn, [repeat_arg]) = take_self_arg.kind;
60-
if is_expr_path_def_path(cx, repeat_fn, &paths::ITER_REPEAT);
60+
if is_path_diagnostic_item(cx, repeat_fn,sym::iter_repeat);
6161
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(collect_expr), sym::String);
6262
if let Some(collect_id) = cx.typeck_results().type_dependent_def_id(collect_expr.hir_id);
6363
if let Some(take_id) = cx.typeck_results().type_dependent_def_id(take_expr.hir_id);

clippy_lints/src/methods/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@ use bind_instead_of_map::BindInsteadOfMap;
102102
use clippy_utils::consts::{constant, Constant};
103103
use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
104104
use clippy_utils::ty::{contains_adt_constructor, implements_trait, is_copy, is_type_diagnostic_item};
105-
use clippy_utils::{
106-
contains_return, get_trait_def_id, is_trait_method, iter_input_pats, meets_msrv, msrvs, paths, return_ty,
107-
};
105+
use clippy_utils::{contains_return, is_trait_method, iter_input_pats, meets_msrv, msrvs, return_ty};
108106
use if_chain::if_chain;
109107
use rustc_hir as hir;
110108
use rustc_hir::def::Res;
@@ -3846,12 +3844,12 @@ impl SelfKind {
38463844
return m == mutability && t == parent_ty;
38473845
}
38483846

3849-
let trait_path = match mutability {
3850-
hir::Mutability::Not => &paths::ASREF_TRAIT,
3851-
hir::Mutability::Mut => &paths::ASMUT_TRAIT,
3847+
let trait_sym = match mutability {
3848+
hir::Mutability::Not => sym::AsRef,
3849+
hir::Mutability::Mut => sym::AsMut,
38523850
};
38533851

3854-
let trait_def_id = match get_trait_def_id(cx, trait_path) {
3852+
let trait_def_id = match cx.tcx.get_diagnostic_item(trait_sym) {
38553853
Some(did) => did,
38563854
None => return false,
38573855
};

clippy_lints/src/methods/option_as_ref_deref.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ pub(super) fn check<'tcx>(
3232
return;
3333
}
3434

35-
let deref_aliases: [&[&str]; 9] = [
36-
&paths::DEREF_TRAIT_METHOD,
35+
let deref_aliases: [&[&str]; 8] = [
3736
&paths::DEREF_MUT_TRAIT_METHOD,
3837
&paths::CSTRING_AS_C_STR,
3938
&paths::OS_STRING_AS_OS_STR,
@@ -45,12 +44,14 @@ pub(super) fn check<'tcx>(
4544
];
4645

4746
let is_deref = match map_arg.kind {
48-
hir::ExprKind::Path(ref expr_qpath) => cx
49-
.qpath_res(expr_qpath, map_arg.hir_id)
50-
.opt_def_id()
51-
.map_or(false, |fun_def_id| {
52-
deref_aliases.iter().any(|path| match_def_path(cx, fun_def_id, path))
53-
}),
47+
hir::ExprKind::Path(ref expr_qpath) => {
48+
cx.qpath_res(expr_qpath, map_arg.hir_id)
49+
.opt_def_id()
50+
.map_or(false, |fun_def_id| {
51+
cx.tcx.is_diagnostic_item(sym::deref_method, fun_def_id)
52+
|| deref_aliases.iter().any(|path| match_def_path(cx, fun_def_id, path))
53+
})
54+
},
5455
hir::ExprKind::Closure(&hir::Closure { body, .. }) => {
5556
let closure_body = cx.tcx.hir().body(body);
5657
let closure_expr = peel_blocks(closure_body.value);
@@ -68,7 +69,8 @@ pub(super) fn check<'tcx>(
6869
if let [ty::adjustment::Adjust::Deref(None), ty::adjustment::Adjust::Borrow(_)] = *adj;
6970
then {
7071
let method_did = cx.typeck_results().type_dependent_def_id(closure_expr.hir_id).unwrap();
71-
deref_aliases.iter().any(|path| match_def_path(cx, method_did, path))
72+
cx.tcx.is_diagnostic_item(sym::deref_method, method_did)
73+
|| deref_aliases.iter().any(|path| match_def_path(cx, method_did, path))
7274
} else {
7375
false
7476
}

0 commit comments

Comments
 (0)