Skip to content

Commit 7d31846

Browse files
committed
Fix adjacent code
1 parent e9919a6 commit 7d31846

17 files changed

+118
-105
lines changed

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,14 +7,14 @@ 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;
1616
use clippy_utils::visitors::for_each_expr;
17-
use clippy_utils::{match_def_path, return_ty, trait_ref_of_method};
17+
use clippy_utils::{return_ty, trait_ref_of_method};
1818

1919
use core::ops::ControlFlow;
2020

@@ -181,15 +181,17 @@ fn is_mutable_pat(cx: &LateContext<'_>, pat: &hir::Pat<'_>, tys: &mut DefIdSet)
181181
}
182182
}
183183

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

186186
fn is_mutable_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, span: Span, tys: &mut DefIdSet) -> bool {
187187
match *ty.kind() {
188188
// primitive types are never mutable
189189
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Str => false,
190190
ty::Adt(adt, substs) => {
191191
tys.insert(adt.did()) && !ty.is_freeze(cx.tcx.at(span), cx.param_env)
192-
|| KNOWN_WRAPPER_TYS.iter().any(|path| match_def_path(cx, adt.did(), path))
192+
|| KNOWN_WRAPPER_TYS
193+
.iter()
194+
.any(|&sym| cx.tcx.is_diagnostic_item(sym, adt.did()))
193195
&& substs.types().any(|ty| is_mutable_ty(cx, ty, span, tys))
194196
},
195197
ty::Tuple(substs) => substs.iter().any(|ty| is_mutable_ty(cx, ty, span, tys)),

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/loops/needless_range_loop.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
33
use clippy_utils::source::snippet;
44
use clippy_utils::ty::has_iter_method;
55
use clippy_utils::visitors::is_local_used;
6-
use clippy_utils::{contains_name, higher, is_integer_const, match_trait_method, paths, sugg, SpanlessEq};
6+
use clippy_utils::{contains_name, higher, is_integer_const, sugg, SpanlessEq};
77
use if_chain::if_chain;
88
use rustc_ast::ast;
99
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
@@ -302,8 +302,13 @@ impl<'a, 'tcx> Visitor<'tcx> for VarVisitor<'a, 'tcx> {
302302
if_chain! {
303303
// a range index op
304304
if let ExprKind::MethodCall(meth, args_0, [args_1, ..], _) = &expr.kind;
305-
if (meth.ident.name == sym::index && match_trait_method(self.cx, expr, &paths::INDEX))
306-
|| (meth.ident.name == sym::index_mut && match_trait_method(self.cx, expr, &paths::INDEX_MUT));
305+
if let trait_id = self
306+
.cx
307+
.typeck_results()
308+
.type_dependent_def_id(expr.hir_id)
309+
.and_then(|def_id| self.cx.tcx.trait_of_item(def_id));
310+
if (meth.ident.name == sym::index && trait_id == self.cx.tcx.lang_items().index_trait())
311+
|| (meth.ident.name == sym::index_mut && trait_id == self.cx.tcx.lang_items().index_mut_trait());
307312
if !self.check(args_1, args_0, expr);
308313
then { return }
309314
}

clippy_lints/src/manual_async_fn.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2-
use clippy_utils::match_function_call;
3-
use clippy_utils::paths::FUTURE_FROM_GENERATOR;
2+
use clippy_utils::match_function_call_with_def_id;
43
use clippy_utils::source::{position_before_rarrow, snippet_block, snippet_opt};
54
use if_chain::if_chain;
65
use rustc_errors::Applicability;
@@ -140,9 +139,9 @@ fn future_output_ty<'tcx>(trait_ref: &'tcx TraitRef<'tcx>) -> Option<&'tcx Ty<'t
140139
if args.bindings.len() == 1;
141140
let binding = &args.bindings[0];
142141
if binding.ident.name == sym::Output;
143-
if let TypeBindingKind::Equality{term: Term::Ty(output)} = binding.kind;
142+
if let TypeBindingKind::Equality { term: Term::Ty(output) } = binding.kind;
144143
then {
145-
return Some(output)
144+
return Some(output);
146145
}
147146
}
148147

@@ -175,9 +174,16 @@ fn captures_all_lifetimes(inputs: &[Ty<'_>], output_lifetimes: &[LifetimeName])
175174
fn desugared_async_block<'tcx>(cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) -> Option<&'tcx Body<'tcx>> {
176175
if_chain! {
177176
if let Some(block_expr) = block.expr;
178-
if let Some(args) = match_function_call(cx, block_expr, &FUTURE_FROM_GENERATOR);
177+
if let Some(args) = cx
178+
.tcx
179+
.lang_items()
180+
.from_generator_fn()
181+
.and_then(|def_id| match_function_call_with_def_id(cx, block_expr, def_id));
179182
if args.len() == 1;
180-
if let Expr{kind: ExprKind::Closure(&Closure { body, .. }), ..} = args[0];
183+
if let Expr {
184+
kind: ExprKind::Closure(&Closure { body, .. }),
185+
..
186+
} = args[0];
181187
let closure_body = cx.tcx.hir().body(body);
182188
if closure_body.generator_kind == Some(GeneratorKind::Async(AsyncGeneratorKind::Block));
183189
then {

clippy_lints/src/manual_clamp.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ use std::ops::Deref;
1212

1313
use clippy_utils::{
1414
diagnostics::{span_lint_and_then, span_lint_hir_and_then},
15-
eq_expr_value, get_trait_def_id,
15+
eq_expr_value,
1616
higher::If,
17-
is_diag_trait_item, is_trait_method, meets_msrv, msrvs, path_res, path_to_local_id, paths, peel_blocks,
17+
is_diag_trait_item, is_trait_method, meets_msrv, msrvs, path_res, path_to_local_id, peel_blocks,
1818
peel_blocks_with_stmt,
1919
sugg::Sugg,
2020
ty::implements_trait,
@@ -190,7 +190,11 @@ impl TypeClampability {
190190
fn is_clampable<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<TypeClampability> {
191191
if ty.is_floating_point() {
192192
Some(TypeClampability::Float)
193-
} else if get_trait_def_id(cx, &paths::ORD).map_or(false, |id| implements_trait(cx, ty, id, &[])) {
193+
} else if cx
194+
.tcx
195+
.get_diagnostic_item(sym::Ord)
196+
.map_or(false, |id| implements_trait(cx, ty, id, &[]))
197+
{
194198
Some(TypeClampability::Ord)
195199
} else {
196200
None

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/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
}

clippy_lints/src/methods/or_fun_call.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::eager_or_lazy::switch_to_lazy_eval;
33
use clippy_utils::source::{snippet, snippet_with_macro_callsite};
4-
use clippy_utils::ty::{implements_trait, match_type};
5-
use clippy_utils::{contains_return, is_trait_item, last_path_segment, paths};
4+
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
5+
use clippy_utils::{contains_return, is_trait_item, last_path_segment};
66
use if_chain::if_chain;
77
use rustc_errors::Applicability;
88
use rustc_hir as hir;
99
use rustc_lint::LateContext;
1010
use rustc_span::source_map::Span;
11-
use rustc_span::symbol::{kw, sym};
11+
use rustc_span::symbol::{kw, sym, Symbol};
1212
use std::borrow::Cow;
1313

1414
use super::OR_FUN_CALL;
@@ -88,11 +88,11 @@ pub(super) fn check<'tcx>(
8888
fun_span: Option<Span>,
8989
) {
9090
// (path, fn_has_argument, methods, suffix)
91-
const KNOW_TYPES: [(&[&str], bool, &[&str], &str); 4] = [
92-
(&paths::BTREEMAP_ENTRY, false, &["or_insert"], "with"),
93-
(&paths::HASHMAP_ENTRY, false, &["or_insert"], "with"),
94-
(&paths::OPTION, false, &["map_or", "ok_or", "or", "unwrap_or"], "else"),
95-
(&paths::RESULT, true, &["or", "unwrap_or"], "else"),
91+
const KNOW_TYPES: [(Symbol, bool, &[&str], &str); 4] = [
92+
(sym::BTreeEntry, false, &["or_insert"], "with"),
93+
(sym::HashMapEntry, false, &["or_insert"], "with"),
94+
(sym::Option, false, &["map_or", "ok_or", "or", "unwrap_or"], "else"),
95+
(sym::Result, true, &["or", "unwrap_or"], "else"),
9696
];
9797

9898
if_chain! {
@@ -104,7 +104,7 @@ pub(super) fn check<'tcx>(
104104
let self_ty = cx.typeck_results().expr_ty(self_expr);
105105

106106
if let Some(&(_, fn_has_arguments, poss, suffix)) =
107-
KNOW_TYPES.iter().find(|&&i| match_type(cx, self_ty, i.0));
107+
KNOW_TYPES.iter().find(|&&i| is_type_diagnostic_item(cx, self_ty, i.0));
108108

109109
if poss.contains(&name);
110110

0 commit comments

Comments
 (0)