Skip to content

Commit e7682a4

Browse files
committed
Provide different suggestions for constructors.
1 parent a737ba8 commit e7682a4

19 files changed

+161
-124
lines changed

clippy_lints/src/derive.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_note, span_lint_and_then};
22
use clippy_utils::paths;
33
use clippy_utils::ty::{implements_trait, is_copy};
4-
use clippy_utils::{get_trait_def_id, is_automatically_derived, is_diagnostic_item, is_lint_allowed, match_def_path};
4+
use clippy_utils::{get_trait_def_id, is_automatically_derived, is_lint_allowed, match_def_path};
55
use if_chain::if_chain;
66
use rustc_hir::intravisit::{walk_expr, walk_fn, walk_item, FnKind, NestedVisitorMap, Visitor};
77
use rustc_hir::{
@@ -197,7 +197,7 @@ fn check_hash_peq<'tcx>(
197197
if_chain! {
198198
if let Some(peq_trait_def_id) = cx.tcx.lang_items().eq_trait();
199199
if let Some(def_id) = trait_ref.trait_def_id();
200-
if is_diagnostic_item(cx, def_id, sym::Hash);
200+
if cx.tcx.is_diagnostic_item(sym::Hash, def_id);
201201
then {
202202
// Look for the PartialEq implementations for `ty`
203203
cx.tcx.for_each_relevant_impl(peq_trait_def_id, ty, |impl_id| {

clippy_lints/src/drop_forget_ref.rs

+22-36
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use clippy_utils::diagnostics::span_lint_and_note;
2-
use clippy_utils::is_diagnostic_item;
32
use clippy_utils::ty::is_copy;
43
use if_chain::if_chain;
54
use rustc_hir::{Expr, ExprKind};
@@ -118,49 +117,36 @@ declare_lint_pass!(DropForgetRef => [DROP_REF, FORGET_REF, DROP_COPY, FORGET_COP
118117
impl<'tcx> LateLintPass<'tcx> for DropForgetRef {
119118
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
120119
if_chain! {
121-
if let ExprKind::Call(path, args) = expr.kind;
120+
if let ExprKind::Call(path, [arg]) = expr.kind;
122121
if let ExprKind::Path(ref qpath) = path.kind;
123-
if args.len() == 1;
124122
if let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id();
125123
then {
126-
let lint;
127-
let msg;
128-
let arg = &args[0];
129124
let arg_ty = cx.typeck_results().expr_ty(arg);
130125

131-
if let ty::Ref(..) = arg_ty.kind() {
132-
if is_diagnostic_item(cx, def_id, sym::mem_drop) {
133-
lint = DROP_REF;
134-
msg = DROP_REF_SUMMARY.to_string();
135-
} else if is_diagnostic_item(cx, def_id, sym::mem_forget) {
136-
lint = FORGET_REF;
137-
msg = FORGET_REF_SUMMARY.to_string();
138-
} else {
139-
return;
126+
let (lint, msg) = if let ty::Ref(..) = arg_ty.kind() {
127+
match cx.tcx.get_diagnostic_name(def_id) {
128+
Some(sym::mem_drop) => (DROP_REF, DROP_REF_SUMMARY),
129+
Some(sym::mem_forget) => (FORGET_REF, FORGET_REF_SUMMARY),
130+
_ => return,
140131
}
141-
span_lint_and_note(cx,
142-
lint,
143-
expr.span,
144-
&msg,
145-
Some(arg.span),
146-
&format!("argument has type `{}`", arg_ty));
147132
} else if is_copy(cx, arg_ty) {
148-
if is_diagnostic_item(cx, def_id, sym::mem_drop) {
149-
lint = DROP_COPY;
150-
msg = DROP_COPY_SUMMARY.to_string();
151-
} else if is_diagnostic_item(cx, def_id, sym::mem_forget) {
152-
lint = FORGET_COPY;
153-
msg = FORGET_COPY_SUMMARY.to_string();
154-
} else {
155-
return;
133+
match cx.tcx.get_diagnostic_name(def_id) {
134+
Some(sym::mem_drop) => (DROP_COPY, DROP_COPY_SUMMARY),
135+
Some(sym::mem_forget) => (FORGET_COPY, FORGET_COPY_SUMMARY),
136+
_ => return,
156137
}
157-
span_lint_and_note(cx,
158-
lint,
159-
expr.span,
160-
&msg,
161-
Some(arg.span),
162-
&format!("argument has type {}", arg_ty));
163-
}
138+
} else {
139+
return;
140+
};
141+
142+
span_lint_and_note(
143+
cx,
144+
lint,
145+
expr.span,
146+
msg,
147+
Some(arg.span),
148+
&format!("argument has type `{}`", arg_ty)
149+
);
164150
}
165151
}
166152
}

clippy_lints/src/infinite_iter.rs

+2-2
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::{get_trait_def_id, higher, is_diagnostic_item, paths};
3+
use clippy_utils::{get_trait_def_id, higher, paths};
44
use rustc_hir::{BorrowKind, Expr, ExprKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -171,7 +171,7 @@ fn is_infinite(cx: &LateContext<'_>, expr: &Expr<'_>) -> Finiteness {
171171
if let ExprKind::Path(ref qpath) = path.kind {
172172
cx.qpath_res(qpath, path.hir_id)
173173
.opt_def_id()
174-
.map_or(false, |id| is_diagnostic_item(cx, id, sym::iter_repeat))
174+
.map_or(false, |id| cx.tcx.is_diagnostic_item(sym::iter_repeat, id))
175175
.into()
176176
} else {
177177
Finite

clippy_lints/src/loops/while_let_on_iterator.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@ use super::WHILE_LET_ON_ITERATOR;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::higher;
44
use clippy_utils::source::snippet_with_applicability;
5-
use clippy_utils::{get_enclosing_loop_or_closure, is_lang_item, is_refutable, is_trait_method, visitors::is_res_used};
5+
use clippy_utils::{get_enclosing_loop_or_closure, is_lang_ctor, is_refutable, is_trait_method, visitors::is_res_used};
66
use if_chain::if_chain;
77
use rustc_errors::Applicability;
88
use rustc_hir::intravisit::{walk_expr, ErasedMap, NestedVisitorMap, Visitor};
9-
use rustc_hir::{def::Res, Expr, ExprKind, HirId, LangItem, Local, PatKind, QPath, UnOp};
9+
use rustc_hir::{def::Res, Expr, ExprKind, HirId, LangItem, Local, PatKind, UnOp};
1010
use rustc_lint::LateContext;
1111
use rustc_span::{symbol::sym, Span, Symbol};
1212

1313
pub(super) fn check(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
1414
let (scrutinee_expr, iter_expr, some_pat, loop_expr) = if_chain! {
1515
if let Some(higher::WhileLet { if_then, let_pat, let_expr }) = higher::WhileLet::hir(expr);
1616
// check for `Some(..)` pattern
17-
if let PatKind::TupleStruct(QPath::Resolved(None, pat_path), some_pat, _) = let_pat.kind;
18-
if let Res::Def(_, pat_did) = pat_path.res;
19-
if is_lang_item(cx, pat_did, LangItem::OptionSome);
17+
if let PatKind::TupleStruct(ref pat_path, some_pat, _) = let_pat.kind;
18+
if is_lang_ctor(cx, pat_path, LangItem::OptionSome);
2019
// check for call to `Iterator::next`
2120
if let ExprKind::MethodCall(method_name, _, [iter_expr], _) = let_expr.kind;
2221
if method_name.ident.name == sym::next;

clippy_lints/src/mem_forget.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use clippy_utils::diagnostics::span_lint;
2-
use clippy_utils::is_diagnostic_item;
32
use rustc_hir::{Expr, ExprKind};
43
use rustc_lint::{LateContext, LateLintPass};
54
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -33,7 +32,7 @@ impl<'tcx> LateLintPass<'tcx> for MemForget {
3332
if let ExprKind::Call(path_expr, [ref first_arg, ..]) = e.kind {
3433
if let ExprKind::Path(ref qpath) = path_expr.kind {
3534
if let Some(def_id) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id() {
36-
if is_diagnostic_item(cx, def_id, sym::mem_forget) {
35+
if cx.tcx.is_diagnostic_item(sym::mem_forget, def_id) {
3736
let forgot_ty = cx.typeck_results().expr_ty(first_arg);
3837

3938
if forgot_ty.ty_adt_def().map_or(false, |def| def.has_dtor(cx.tcx)) {

clippy_lints/src/mem_replace.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg, span_lint_and_then};
22
use clippy_utils::source::{snippet, snippet_with_applicability};
33
use clippy_utils::ty::is_non_aggregate_primitive_type;
4-
use clippy_utils::{is_default_equivalent, is_diagnostic_item, is_lang_ctor, meets_msrv, msrvs};
4+
use clippy_utils::{is_default_equivalent, is_lang_ctor, meets_msrv, msrvs};
55
use if_chain::if_chain;
66
use rustc_errors::Applicability;
77
use rustc_hir::LangItem::OptionNone;
@@ -249,7 +249,7 @@ impl<'tcx> LateLintPass<'tcx> for MemReplace {
249249
if let ExprKind::Call(func, func_args) = expr.kind;
250250
if let ExprKind::Path(ref func_qpath) = func.kind;
251251
if let Some(def_id) = cx.qpath_res(func_qpath, func.hir_id).opt_def_id();
252-
if is_diagnostic_item(cx, def_id, sym::mem_replace);
252+
if cx.tcx.is_diagnostic_item(sym::mem_replace, def_id);
253253
if let [dest, src] = func_args;
254254
then {
255255
check_replace_option_with_none(cx, src, dest, expr.span);

clippy_lints/src/methods/inefficient_to_string.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::snippet_with_applicability;
33
use clippy_utils::ty::{is_type_diagnostic_item, walk_ptrs_ty_depth};
4-
use clippy_utils::{is_diagnostic_item, match_def_path, paths};
4+
use clippy_utils::{match_def_path, paths};
55
use if_chain::if_chain;
66
use rustc_errors::Applicability;
77
use rustc_hir as hir;
@@ -60,7 +60,7 @@ fn specializes_tostring(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
6060
}
6161

6262
if let ty::Adt(adt, substs) = ty.kind() {
63-
is_diagnostic_item(cx, adt.did, sym::Cow) && substs.type_at(1).is_str()
63+
cx.tcx.is_diagnostic_item(sym::Cow, adt.did) && substs.type_at(1).is_str()
6464
} else {
6565
false
6666
}

clippy_lints/src/minmax.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::consts::{constant_simple, Constant};
22
use clippy_utils::diagnostics::span_lint;
3-
use clippy_utils::{is_diagnostic_item, is_trait_method};
3+
use clippy_utils::is_trait_method;
44
use if_chain::if_chain;
55
use rustc_hir::{Expr, ExprKind};
66
use rustc_lint::{LateContext, LateLintPass};
@@ -74,14 +74,10 @@ fn min_max<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<(MinMax, Cons
7474
cx.typeck_results()
7575
.qpath_res(qpath, path.hir_id)
7676
.opt_def_id()
77-
.and_then(|def_id| {
78-
if is_diagnostic_item(cx, def_id, sym::cmp_min) {
79-
fetch_const(cx, args, MinMax::Min)
80-
} else if is_diagnostic_item(cx, def_id, sym::cmp_max) {
81-
fetch_const(cx, args, MinMax::Max)
82-
} else {
83-
None
84-
}
77+
.and_then(|def_id| match cx.tcx.get_diagnostic_name(def_id) {
78+
Some(sym::cmp_min) => fetch_const(cx, args, MinMax::Min),
79+
Some(sym::cmp_max) => fetch_const(cx, args, MinMax::Max),
80+
_ => None,
8581
})
8682
} else {
8783
None

clippy_lints/src/redundant_clone.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::{span_lint_hir, span_lint_hir_and_then};
22
use clippy_utils::source::snippet_opt;
33
use clippy_utils::ty::{has_drop, is_copy, is_type_diagnostic_item, walk_ptrs_ty_depth};
4-
use clippy_utils::{fn_has_unsatisfiable_preds, is_lang_item, match_def_path, paths};
4+
use clippy_utils::{fn_has_unsatisfiable_preds, match_def_path, paths};
55
use if_chain::if_chain;
66
use rustc_data_structures::{fx::FxHashMap, transitive_relation::TransitiveRelation};
77
use rustc_errors::Applicability;
@@ -135,7 +135,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClone {
135135
}
136136

137137
if let ty::Adt(def, _) = arg_ty.kind() {
138-
if is_lang_item(cx, def.did, LangItem::ManuallyDrop) {
138+
if cx.tcx.lang_items().require(LangItem::ManuallyDrop).ok() == Some(def.did) {
139139
continue;
140140
}
141141
}

clippy_lints/src/size_of_in_element_count.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! expecting a count of T
33
44
use clippy_utils::diagnostics::span_lint_and_help;
5-
use clippy_utils::{is_diagnostic_item, match_def_path, paths};
5+
use clippy_utils::{match_def_path, paths};
66
use if_chain::if_chain;
77
use rustc_hir::BinOpKind;
88
use rustc_hir::{Expr, ExprKind};
@@ -45,8 +45,7 @@ fn get_size_of_ty(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, inverted: bool)
4545
if !inverted;
4646
if let ExprKind::Path(ref count_func_qpath) = count_func.kind;
4747
if let Some(def_id) = cx.qpath_res(count_func_qpath, count_func.hir_id).opt_def_id();
48-
if is_diagnostic_item(cx, def_id, sym::mem_size_of)
49-
|| is_diagnostic_item(cx, def_id, sym::mem_size_of_val);
48+
if matches!(cx.tcx.get_diagnostic_name(def_id), Some(sym::mem_size_of | sym::mem_size_of_val));
5049
then {
5150
cx.typeck_results().node_substs(count_func.hir_id).types().next()
5251
} else {

clippy_lints/src/to_string_in_display.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint;
2-
use clippy_utils::{is_diag_trait_item, is_diagnostic_item, path_to_local_id};
2+
use clippy_utils::{is_diag_trait_item, path_to_local_id};
33
use if_chain::if_chain;
44
use rustc_hir::{Expr, ExprKind, HirId, Impl, ImplItem, ImplItemKind, Item, ItemKind};
55
use rustc_lint::{LateContext, LateLintPass};
@@ -115,7 +115,7 @@ fn is_display_impl(cx: &LateContext<'_>, item: &Item<'_>) -> bool {
115115
if let ItemKind::Impl(Impl { of_trait: Some(trait_ref), .. }) = &item.kind;
116116
if let Some(did) = trait_ref.trait_def_id();
117117
then {
118-
is_diagnostic_item(cx, did, sym::Display)
118+
cx.tcx.is_diagnostic_item(sym::Display, did)
119119
} else {
120120
false
121121
}

clippy_lints/src/types/borrowed_box.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::is_diagnostic_item;
32
use clippy_utils::source::snippet;
43

54
use if_chain::if_chain;
@@ -91,7 +90,7 @@ fn is_any_trait(cx: &LateContext<'_>, t: &hir::Ty<'_>) -> bool {
9190
if let Some(trait_did) = traits[0].trait_ref.trait_def_id();
9291
// Only Send/Sync can be used as additional traits, so it is enough to
9392
// check only the first trait.
94-
if is_diagnostic_item(cx, trait_did, sym::Any);
93+
if cx.tcx.is_diagnostic_item(sym::Any, trait_did);
9594
then {
9695
return true;
9796
}

clippy_lints/src/useless_conversion.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
22
use clippy_utils::source::{snippet, snippet_with_macro_callsite};
33
use clippy_utils::sugg::Sugg;
44
use clippy_utils::ty::{is_type_diagnostic_item, same_type_and_consts};
5-
use clippy_utils::{get_parent_expr, is_lang_item, is_trait_method, match_def_path, paths};
5+
use clippy_utils::{get_parent_expr, is_trait_method, match_def_path, paths};
66
use if_chain::if_chain;
77
use rustc_errors::Applicability;
88
use rustc_hir::{Expr, ExprKind, HirId, LangItem, MatchSource};
@@ -154,7 +154,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
154154
}
155155

156156
if_chain! {
157-
if is_lang_item(cx, def_id, LangItem::FromFrom);
157+
if cx.tcx.lang_items().require(LangItem::FromFrom).ok() == Some(def_id);
158158
if same_type_and_consts(a, b);
159159

160160
then {

0 commit comments

Comments
 (0)