Skip to content

Commit f1ce294

Browse files
committed
clippy: support QPath::LangItem
This commit updates clippy with the introduction of `QPath::LangItem` so that it still compiles. Signed-off-by: David Wood <[email protected]>
1 parent dde93c9 commit f1ce294

20 files changed

+117
-130
lines changed

src/tools/clippy/clippy_lints/src/default_trait_access.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl<'tcx> LateLintPass<'tcx> for DefaultTraitAccess {
6868
);
6969
}
7070
},
71-
QPath::TypeRelative(..) => {},
71+
QPath::TypeRelative(..) | QPath::LangItem(..) => {},
7272
}
7373
}
7474
}

src/tools/clippy/clippy_lints/src/indexing_slicing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
8989
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
9090
if let ExprKind::Index(ref array, ref index) = &expr.kind {
9191
let ty = cx.typeck_results().expr_ty(array);
92-
if let Some(range) = higher::range(cx, index) {
92+
if let Some(range) = higher::range(index) {
9393
// Ranged indexes, i.e., &x[n..m], &x[n..], &x[..n] and &x[..]
9494
if let ty::Array(_, s) = ty.kind {
9595
let size: u128 = if let Some(size) = s.try_eval_usize(cx.tcx, cx.param_env) {

src/tools/clippy/clippy_lints/src/infinite_iter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ fn is_infinite(cx: &LateContext<'_>, expr: &Expr<'_>) -> Finiteness {
171171
Finite
172172
}
173173
},
174-
ExprKind::Struct(..) => higher::range(cx, expr).map_or(false, |r| r.end.is_none()).into(),
174+
ExprKind::Struct(..) => higher::range(expr).map_or(false, |r| r.end.is_none()).into(),
175175
_ => Finite,
176176
}
177177
}

src/tools/clippy/clippy_lints/src/len_zero.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ fn check_len(
262262
fn has_is_empty(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
263263
/// Special case ranges until `range_is_empty` is stabilized. See issue 3807.
264264
fn should_skip_range(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
265-
higher::range(cx, expr).map_or(false, |_| {
265+
higher::range(expr).map_or(false, |_| {
266266
!cx.tcx
267267
.features()
268268
.declared_lib_features

src/tools/clippy/clippy_lints/src/loops.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ fn detect_manual_memcpy<'tcx>(
10031003
start: Some(start),
10041004
end: Some(end),
10051005
limits,
1006-
}) = higher::range(cx, arg)
1006+
}) = higher::range(arg)
10071007
{
10081008
// the var must be a single name
10091009
if let PatKind::Binding(_, canonical_id, _, _) = pat.kind {
@@ -1177,7 +1177,7 @@ fn check_for_loop_range<'tcx>(
11771177
start: Some(start),
11781178
ref end,
11791179
limits,
1180-
}) = higher::range(cx, arg)
1180+
}) = higher::range(arg)
11811181
{
11821182
// the var must be a single name
11831183
if let PatKind::Binding(_, canonical_id, ident, _) = pat.kind {
@@ -1679,7 +1679,7 @@ fn check_for_mut_range_bound(cx: &LateContext<'_>, arg: &Expr<'_>, body: &Expr<'
16791679
start: Some(start),
16801680
end: Some(end),
16811681
..
1682-
}) = higher::range(cx, arg)
1682+
}) = higher::range(arg)
16831683
{
16841684
let mut_ids = vec![check_for_mutability(cx, start), check_for_mutability(cx, end)];
16851685
if mut_ids[0].is_some() || mut_ids[1].is_some() {

src/tools/clippy/clippy_lints/src/match_on_vec_items.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use crate::utils::{self, is_type_diagnostic_item, match_type, snippet, span_lint_and_sugg, walk_ptrs_ty};
1+
use crate::utils::{is_type_diagnostic_item, is_type_lang_item, snippet, span_lint_and_sugg};
2+
use crate::utils::walk_ptrs_ty;
23
use if_chain::if_chain;
34
use rustc_errors::Applicability;
4-
use rustc_hir::{Expr, ExprKind, MatchSource};
5+
use rustc_hir::{Expr, ExprKind, LangItem, MatchSource};
56
use rustc_lint::{LateContext, LateLintPass, LintContext};
67
use rustc_middle::lint::in_external_macro;
78
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -96,5 +97,5 @@ fn is_vector(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
9697
fn is_full_range(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
9798
let ty = cx.typeck_results().expr_ty(expr);
9899
let ty = walk_ptrs_ty(ty);
99-
match_type(cx, ty, &utils::paths::RANGE_FULL)
100+
is_type_lang_item(cx, ty, LangItem::RangeFull)
100101
}

src/tools/clippy/clippy_lints/src/methods/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2271,7 +2271,7 @@ fn lint_iter_next<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, iter_
22712271
if_chain! {
22722272
if let hir::ExprKind::Index(ref caller_var, ref index_expr) = &caller_expr.kind;
22732273
if let Some(higher::Range { start: Some(start_expr), end: None, limits: ast::RangeLimits::HalfOpen })
2274-
= higher::range(cx, index_expr);
2274+
= higher::range(index_expr);
22752275
if let hir::ExprKind::Lit(ref start_lit) = &start_expr.kind;
22762276
if let ast::LitKind::Int(start_idx, _) = start_lit.node;
22772277
then {

src/tools/clippy/clippy_lints/src/misc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
433433
return;
434434
}
435435
let binding = match expr.kind {
436+
ExprKind::Path(hir::QPath::LangItem(..)) => None,
436437
ExprKind::Path(ref qpath) => {
437438
let binding = last_path_segment(qpath).ident.as_str();
438439
if binding.starts_with('_') &&

src/tools/clippy/clippy_lints/src/ranges.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl<'tcx> LateLintPass<'tcx> for Ranges {
147147
if let ExprKind::MethodCall(ref iter_path, _, ref iter_args , _) = *iter;
148148
if iter_path.ident.name == sym!(iter);
149149
// range expression in `.zip()` call: `0..x.len()`
150-
if let Some(higher::Range { start: Some(start), end: Some(end), .. }) = higher::range(cx, zip_arg);
150+
if let Some(higher::Range { start: Some(start), end: Some(end), .. }) = higher::range(zip_arg);
151151
if is_integer_const(cx, start, 0);
152152
// `.len()` call
153153
if let ExprKind::MethodCall(ref len_path, _, ref len_args, _) = end.kind;
@@ -180,7 +180,7 @@ fn check_exclusive_range_plus_one(cx: &LateContext<'_>, expr: &Expr<'_>) {
180180
start,
181181
end: Some(end),
182182
limits: RangeLimits::HalfOpen
183-
}) = higher::range(cx, expr);
183+
}) = higher::range(expr);
184184
if let Some(y) = y_plus_one(cx, end);
185185
then {
186186
let span = if expr.span.from_expansion() {
@@ -225,7 +225,7 @@ fn check_exclusive_range_plus_one(cx: &LateContext<'_>, expr: &Expr<'_>) {
225225
// inclusive range minus one: `x..=(y-1)`
226226
fn check_inclusive_range_minus_one(cx: &LateContext<'_>, expr: &Expr<'_>) {
227227
if_chain! {
228-
if let Some(higher::Range { start, end: Some(end), limits: RangeLimits::Closed }) = higher::range(cx, expr);
228+
if let Some(higher::Range { start, end: Some(end), limits: RangeLimits::Closed }) = higher::range(expr);
229229
if let Some(y) = y_minus_one(cx, end);
230230
then {
231231
span_lint_and_then(
@@ -279,7 +279,7 @@ fn check_reversed_empty_range(cx: &LateContext<'_>, expr: &Expr<'_>) {
279279
}
280280

281281
if_chain! {
282-
if let Some(higher::Range { start: Some(start), end: Some(end), limits }) = higher::range(cx, expr);
282+
if let Some(higher::Range { start: Some(start), end: Some(end), limits }) = higher::range(expr);
283283
let ty = cx.typeck_results().expr_ty(start);
284284
if let ty::Int(_) | ty::Uint(_) = ty.kind;
285285
if let Some((start_idx, _)) = constant(cx, cx.typeck_results(), start);

src/tools/clippy/clippy_lints/src/try_err.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::utils::{
2-
is_type_diagnostic_item, match_def_path, match_qpath, paths, snippet, snippet_with_macro_callsite,
3-
span_lint_and_sugg,
2+
is_type_diagnostic_item, match_def_path, match_qpath, paths, snippet,
3+
snippet_with_macro_callsite, span_lint_and_sugg,
44
};
55
use if_chain::if_chain;
66
use rustc_errors::Applicability;
7-
use rustc_hir::{Expr, ExprKind, MatchSource};
7+
use rustc_hir::{Expr, ExprKind, QPath, LangItem, MatchSource};
88
use rustc_lint::{LateContext, LateLintPass};
99
use rustc_middle::lint::in_external_macro;
1010
use rustc_middle::ty::{self, Ty};
@@ -62,7 +62,7 @@ impl<'tcx> LateLintPass<'tcx> for TryErr {
6262
if let ExprKind::Match(ref match_arg, _, MatchSource::TryDesugar) = expr.kind;
6363
if let ExprKind::Call(ref match_fun, ref try_args) = match_arg.kind;
6464
if let ExprKind::Path(ref match_fun_path) = match_fun.kind;
65-
if match_qpath(match_fun_path, &paths::TRY_INTO_RESULT);
65+
if matches!(match_fun_path, QPath::LangItem(LangItem::TryIntoResult, _));
6666
if let Some(ref try_arg) = try_args.get(0);
6767
if let ExprKind::Call(ref err_fun, ref err_args) = try_arg.kind;
6868
if let Some(ref err_arg) = err_args.get(0);

src/tools/clippy/clippy_lints/src/types.rs

+1
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ impl Types {
475475
}
476476
}
477477
},
478+
QPath::LangItem(..) => {},
478479
}
479480
},
480481
TyKind::Rptr(ref lt, ref mut_ty) => self.check_ty_rptr(cx, hir_ty, is_local, lt, mut_ty),

src/tools/clippy/clippy_lints/src/unused_io_amount.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{is_try, match_qpath, match_trait_method, paths, span_lint};
1+
use crate::utils::{is_try, match_trait_method, paths, span_lint};
22
use rustc_hir as hir;
33
use rustc_lint::{LateContext, LateLintPass};
44
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -42,10 +42,11 @@ impl<'tcx> LateLintPass<'tcx> for UnusedIoAmount {
4242
match expr.kind {
4343
hir::ExprKind::Match(ref res, _, _) if is_try(expr).is_some() => {
4444
if let hir::ExprKind::Call(ref func, ref args) = res.kind {
45-
if let hir::ExprKind::Path(ref path) = func.kind {
46-
if match_qpath(path, &paths::TRY_INTO_RESULT) && args.len() == 1 {
47-
check_method_call(cx, &args[0], expr);
48-
}
45+
if matches!(
46+
func.kind,
47+
hir::ExprKind::Path(hir::QPath::LangItem(hir::LangItem::TryIntoResult, _))
48+
) {
49+
check_method_call(cx, &args[0], expr);
4950
}
5051
} else {
5152
check_method_call(cx, res, expr);

src/tools/clippy/clippy_lints/src/utils/author.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,19 @@ impl PrintVisitor {
175175
}
176176

177177
fn print_qpath(&mut self, path: &QPath<'_>) {
178-
print!(" if match_qpath({}, &[", self.current);
179-
print_path(path, &mut true);
180-
println!("]);");
178+
match *path {
179+
QPath::LangItem(lang_item, _) => {
180+
println!(
181+
" if matches!({}, QPath::LangItem(LangItem::{:?}, _));",
182+
self.current, lang_item,
183+
);
184+
},
185+
_ => {
186+
print!(" if match_qpath({}, &[", self.current);
187+
print_path(path, &mut true);
188+
println!("]);");
189+
},
190+
}
181191
}
182192
}
183193

@@ -760,5 +770,6 @@ fn print_path(path: &QPath<'_>, first: &mut bool) {
760770
},
761771
ref other => print!("/* unimplemented: {:?}*/", other),
762772
},
773+
QPath::LangItem(..) => panic!("print_path: called for lang item qpath"),
763774
}
764775
}

src/tools/clippy/clippy_lints/src/utils/higher.rs

+23-75
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
44
#![deny(clippy::missing_docs_in_private_items)]
55

6-
use crate::utils::{is_expn_of, match_def_path, match_qpath, paths};
6+
use crate::utils::{is_expn_of, match_def_path, paths};
77
use if_chain::if_chain;
88
use rustc_ast::ast;
99
use rustc_hir as hir;
1010
use rustc_lint::LateContext;
11-
use rustc_middle::ty;
1211

1312
/// Converts a hir binary operator to the corresponding `ast` type.
1413
#[must_use]
@@ -47,7 +46,7 @@ pub struct Range<'a> {
4746
}
4847

4948
/// Higher a `hir` range to something similar to `ast::ExprKind::Range`.
50-
pub fn range<'a, 'tcx>(cx: &LateContext<'tcx>, expr: &'a hir::Expr<'_>) -> Option<Range<'a>> {
49+
pub fn range<'a>(expr: &'a hir::Expr<'_>) -> Option<Range<'a>> {
5150
/// Finds the field named `name` in the field. Always return `Some` for
5251
/// convenience.
5352
fn get_field<'c>(name: &str, fields: &'c [hir::Field<'_>]) -> Option<&'c hir::Expr<'c>> {
@@ -56,94 +55,43 @@ pub fn range<'a, 'tcx>(cx: &LateContext<'tcx>, expr: &'a hir::Expr<'_>) -> Optio
5655
Some(expr)
5756
}
5857

59-
let def_path = match cx.typeck_results().expr_ty(expr).kind {
60-
ty::Adt(def, _) => cx.tcx.def_path(def.did),
61-
_ => return None,
62-
};
63-
64-
// sanity checks for std::ops::RangeXXXX
65-
if def_path.data.len() != 3 {
66-
return None;
67-
}
68-
if def_path.data.get(0)?.data.as_symbol() != sym!(ops) {
69-
return None;
70-
}
71-
if def_path.data.get(1)?.data.as_symbol() != sym!(range) {
72-
return None;
73-
}
74-
let type_name = def_path.data.get(2)?.data.as_symbol();
75-
let range_types = [
76-
"RangeFrom",
77-
"RangeFull",
78-
"RangeInclusive",
79-
"Range",
80-
"RangeTo",
81-
"RangeToInclusive",
82-
];
83-
if !range_types.contains(&&*type_name.as_str()) {
84-
return None;
85-
}
86-
87-
// The range syntax is expanded to literal paths starting with `core` or `std`
88-
// depending on
89-
// `#[no_std]`. Testing both instead of resolving the paths.
90-
9158
match expr.kind {
92-
hir::ExprKind::Path(ref path) => {
93-
if match_qpath(path, &paths::RANGE_FULL_STD) || match_qpath(path, &paths::RANGE_FULL) {
94-
Some(Range {
59+
hir::ExprKind::Call(ref path, ref args) if matches!(
60+
path.kind,
61+
hir::ExprKind::Path(hir::QPath::LangItem(hir::LangItem::RangeInclusiveNew, _))
62+
) => Some(Range {
63+
start: Some(&args[0]),
64+
end: Some(&args[1]),
65+
limits: ast::RangeLimits::Closed,
66+
}),
67+
hir::ExprKind::Struct(ref path, ref fields, None) => {
68+
match path {
69+
hir::QPath::LangItem(hir::LangItem::RangeFull, _) => Some(Range {
9570
start: None,
9671
end: None,
9772
limits: ast::RangeLimits::HalfOpen,
98-
})
99-
} else {
100-
None
101-
}
102-
},
103-
hir::ExprKind::Call(ref path, ref args) => {
104-
if let hir::ExprKind::Path(ref path) = path.kind {
105-
if match_qpath(path, &paths::RANGE_INCLUSIVE_STD_NEW) || match_qpath(path, &paths::RANGE_INCLUSIVE_NEW)
106-
{
107-
Some(Range {
108-
start: Some(&args[0]),
109-
end: Some(&args[1]),
110-
limits: ast::RangeLimits::Closed,
111-
})
112-
} else {
113-
None
114-
}
115-
} else {
116-
None
117-
}
118-
},
119-
hir::ExprKind::Struct(ref path, ref fields, None) => {
120-
if match_qpath(path, &paths::RANGE_FROM_STD) || match_qpath(path, &paths::RANGE_FROM) {
121-
Some(Range {
73+
}),
74+
hir::QPath::LangItem(hir::LangItem::RangeFrom, _) => Some(Range {
12275
start: Some(get_field("start", fields)?),
12376
end: None,
12477
limits: ast::RangeLimits::HalfOpen,
125-
})
126-
} else if match_qpath(path, &paths::RANGE_STD) || match_qpath(path, &paths::RANGE) {
127-
Some(Range {
78+
}),
79+
hir::QPath::LangItem(hir::LangItem::Range, _) => Some(Range {
12880
start: Some(get_field("start", fields)?),
12981
end: Some(get_field("end", fields)?),
13082
limits: ast::RangeLimits::HalfOpen,
131-
})
132-
} else if match_qpath(path, &paths::RANGE_TO_INCLUSIVE_STD) || match_qpath(path, &paths::RANGE_TO_INCLUSIVE)
133-
{
134-
Some(Range {
83+
}),
84+
hir::QPath::LangItem(hir::LangItem::RangeToInclusive, _) => Some(Range {
13585
start: None,
13686
end: Some(get_field("end", fields)?),
13787
limits: ast::RangeLimits::Closed,
138-
})
139-
} else if match_qpath(path, &paths::RANGE_TO_STD) || match_qpath(path, &paths::RANGE_TO) {
140-
Some(Range {
88+
}),
89+
hir::QPath::LangItem(hir::LangItem::RangeTo, _) => Some(Range {
14190
start: None,
14291
end: Some(get_field("end", fields)?),
14392
limits: ast::RangeLimits::HalfOpen,
144-
})
145-
} else {
146-
None
93+
}),
94+
_ => None,
14795
}
14896
},
14997
_ => None,

0 commit comments

Comments
 (0)