Skip to content

Commit 6572dc1

Browse files
committed
Fix clippy after lang item refactor
1 parent 16bfead commit 6572dc1

19 files changed

+107
-137
lines changed

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_applicability;
33
use clippy_utils::ty::is_type_diagnostic_item;
4-
use clippy_utils::{get_parent_expr, is_res_lang_ctor, match_def_path, path_res, paths};
4+
use clippy_utils::{get_parent_expr, is_path_lang_item, is_res_lang_ctor, match_def_path, path_res, paths};
55
use if_chain::if_chain;
66
use rustc_errors::Applicability;
77
use rustc_hir::LangItem::ResultErr;
8-
use rustc_hir::{Expr, ExprKind, LangItem, MatchSource, QPath};
8+
use rustc_hir::{Expr, ExprKind, LangItem, MatchSource};
99
use rustc_lint::LateContext;
1010
use rustc_middle::ty::{self, Ty};
1111
use rustc_span::{hygiene, sym};
@@ -24,8 +24,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, scrutine
2424
// };
2525
if_chain! {
2626
if let ExprKind::Call(match_fun, [try_arg, ..]) = scrutinee.kind;
27-
if let ExprKind::Path(ref match_fun_path) = match_fun.kind;
28-
if matches!(match_fun_path, QPath::LangItem(LangItem::TryTraitBranch, ..));
27+
if is_path_lang_item(cx, match_fun, LangItem::TryTraitBranch);
2928
if let ExprKind::Call(err_fun, [err_arg, ..]) = try_arg.kind;
3029
if is_res_lang_ctor(cx, path_res(cx, err_fun), ResultErr);
3130
if let Some(return_ty) = find_return_type(cx, &expr.kind);

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

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
2-
use clippy_utils::get_parent_node;
32
use clippy_utils::source::snippet_with_context;
43
use clippy_utils::sugg;
54
use clippy_utils::ty::is_copy;
5+
use clippy_utils::{get_parent_node, is_path_lang_item};
66
use rustc_errors::Applicability;
7-
use rustc_hir::{BindingAnnotation, ByRef, Expr, ExprKind, MatchSource, Node, PatKind, QPath};
7+
use rustc_hir::{BindingAnnotation, ByRef, Expr, ExprKind, LangItem, MatchSource, Node, PatKind};
88
use rustc_lint::LateContext;
99
use rustc_middle::ty::{self, adjustment::Adjust};
1010
use rustc_span::symbol::{sym, Symbol};
@@ -93,10 +93,7 @@ pub(super) fn check(
9393
return;
9494
},
9595
// ? is a Call, makes sure not to rec *x?, but rather (*x)?
96-
ExprKind::Call(hir_callee, _) => matches!(
97-
hir_callee.kind,
98-
ExprKind::Path(QPath::LangItem(rustc_hir::LangItem::TryTraitBranch, _, _))
99-
),
96+
ExprKind::Call(hir_callee, _) => is_path_lang_item(cx, hir_callee, LangItem::TryTraitBranch),
10097
ExprKind::MethodCall(_, self_arg, ..) if expr.hir_id == self_arg.hir_id => true,
10198
ExprKind::Match(_, _, MatchSource::TryDesugar | MatchSource::AwaitDesugar)
10299
| ExprKind::Field(..)

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ use rustc_middle::lint::in_external_macro;
113113
use rustc_middle::ty::{self, TraitRef, Ty};
114114
use rustc_semver::RustcVersion;
115115
use rustc_session::{declare_tool_lint, impl_lint_pass};
116-
use rustc_span::{sym, Span};
116+
use rustc_span::{sym, DesugaringKind, Span};
117117

118118
declare_clippy_lint! {
119119
/// ### What it does
@@ -3197,7 +3197,11 @@ fn method_call<'tcx>(
31973197
recv: &'tcx hir::Expr<'tcx>,
31983198
) -> Option<(&'tcx str, &'tcx hir::Expr<'tcx>, &'tcx [hir::Expr<'tcx>], Span)> {
31993199
if let ExprKind::MethodCall(path, receiver, args, _) = recv.kind {
3200-
if !args.iter().any(|e| e.span.from_expansion()) && !receiver.span.from_expansion() {
3200+
if !(args
3201+
.iter()
3202+
.any(|e| e.span.from_expansion() && !e.span.is_desugaring(DesugaringKind::RangeLiteral))
3203+
|| (receiver.span.from_expansion() && !receiver.span.is_desugaring(DesugaringKind::RangeLiteral)))
3204+
{
32013205
let name = path.ident.name.as_str();
32023206
return Some((name, receiver, args, path.ident.span));
32033207
}

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

+5-9
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
33
use clippy_utils::source::snippet_with_context;
44
use clippy_utils::usage::local_used_after_expr;
55
use clippy_utils::visitors::{for_each_expr_with_closures, Descend};
6-
use clippy_utils::{is_diag_item_method, match_def_path, meets_msrv, msrvs, path_to_local_id, paths};
6+
use clippy_utils::{
7+
is_diag_item_method, is_path_lang_item, match_def_path, meets_msrv, msrvs, path_to_local_id, paths,
8+
};
79
use core::ops::ControlFlow;
810
use if_chain::if_chain;
911
use rustc_errors::Applicability;
1012
use rustc_hir::{
11-
BindingAnnotation, Expr, ExprKind, HirId, LangItem, Local, MatchSource, Node, Pat, PatKind, QPath, Stmt, StmtKind,
13+
BindingAnnotation, Expr, ExprKind, HirId, LangItem, Local, MatchSource, Node, Pat, PatKind, Stmt, StmtKind,
1214
};
1315
use rustc_lint::LateContext;
1416
use rustc_middle::ty;
@@ -347,13 +349,7 @@ fn parse_iter_usage<'tcx>(
347349

348350
let (unwrap_kind, span) = if let Some((_, Node::Expr(e))) = iter.next() {
349351
match e.kind {
350-
ExprKind::Call(
351-
Expr {
352-
kind: ExprKind::Path(QPath::LangItem(LangItem::TryTraitBranch, ..)),
353-
..
354-
},
355-
_,
356-
) => {
352+
ExprKind::Call(callee, _) if is_path_lang_item(cx, callee, LangItem::TryTraitBranch) => {
357353
let parent_span = e.span.parent_callsite().unwrap();
358354
if parent_span.ctxt() == ctxt {
359355
(Some(UnwrapKind::QuestionMark), parent_span)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
240240
}
241241
let sym;
242242
let binding = match expr.kind {
243-
ExprKind::Path(ref qpath) if !matches!(qpath, hir::QPath::LangItem(..)) => {
243+
ExprKind::Path(ref qpath) => {
244244
let binding = last_path_segment(qpath).ident.as_str();
245245
if binding.starts_with('_') &&
246246
!binding.starts_with("__") &&

src/tools/clippy/clippy_lints/src/needless_question_mark.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::path_res;
32
use clippy_utils::source::snippet;
3+
use clippy_utils::{is_path_lang_item, path_res};
44
use if_chain::if_chain;
55
use rustc_errors::Applicability;
66
use rustc_hir::def::{DefKind, Res};
7-
use rustc_hir::{AsyncGeneratorKind, Block, Body, Expr, ExprKind, GeneratorKind, LangItem, MatchSource, QPath};
7+
use rustc_hir::{AsyncGeneratorKind, Block, Body, Expr, ExprKind, GeneratorKind, LangItem, MatchSource};
88
use rustc_lint::{LateContext, LateLintPass};
99
use rustc_middle::ty::DefIdTree;
1010
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -124,8 +124,8 @@ fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
124124
return;
125125
};
126126
if let ExprKind::Match(inner_expr_with_q, _, MatchSource::TryDesugar) = &arg.kind;
127-
if let ExprKind::Call(called, [inner_expr]) = &inner_expr_with_q.kind;
128-
if let ExprKind::Path(QPath::LangItem(LangItem::TryTraitBranch, ..)) = &called.kind;
127+
if let ExprKind::Call(called, [inner_expr]) = inner_expr_with_q.kind;
128+
if is_path_lang_item(cx, called, LangItem::TryTraitBranch);
129129
if expr.span.ctxt() == inner_expr.span.ctxt();
130130
let expr_ty = cx.typeck_results().expr_ty(expr);
131131
let inner_ty = cx.typeck_results().expr_ty(inner_expr);

src/tools/clippy/clippy_lints/src/no_effect.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_hir::def::{DefKind, Res};
88
use rustc_hir::{is_range_literal, BinOpKind, BlockCheckMode, Expr, ExprKind, PatKind, Stmt, StmtKind, UnsafeSource};
99
use rustc_lint::{LateContext, LateLintPass};
1010
use rustc_session::{declare_lint_pass, declare_tool_lint};
11+
use rustc_span::DesugaringKind;
1112
use std::ops::Deref;
1213

1314
declare_clippy_lint! {
@@ -113,7 +114,7 @@ fn check_no_effect(cx: &LateContext<'_>, stmt: &Stmt<'_>) -> bool {
113114
}
114115

115116
fn has_no_effect(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
116-
if expr.span.from_expansion() {
117+
if expr.span.from_expansion() && !is_range_literal(expr) {
117118
return false;
118119
}
119120
match peel_blocks(expr).kind {
@@ -160,7 +161,7 @@ fn check_unnecessary_operation(cx: &LateContext<'_>, stmt: &Stmt<'_>) {
160161
if_chain! {
161162
if let StmtKind::Semi(expr) = stmt.kind;
162163
if let Some(reduced) = reduce_expression(cx, expr);
163-
if !&reduced.iter().any(|e| e.span.from_expansion());
164+
if !&reduced.iter().any(|e| e.span.from_expansion() && !e.span.is_desugaring(DesugaringKind::RangeLiteral));
164165
then {
165166
if let ExprKind::Index(..) = &expr.kind {
166167
let snippet = if let (Some(arr), Some(func)) =
@@ -216,7 +217,7 @@ fn check_unnecessary_operation(cx: &LateContext<'_>, stmt: &Stmt<'_>) {
216217
}
217218

218219
fn reduce_expression<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<Vec<&'a Expr<'a>>> {
219-
if expr.span.from_expansion() {
220+
if expr.span.from_expansion() && !expr.span.is_desugaring(DesugaringKind::RangeLiteral) {
220221
return None;
221222
}
222223
match expr.kind {

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

+22-27
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_lint::{LateContext, LateLintPass};
1212
use rustc_middle::ty;
1313
use rustc_semver::RustcVersion;
1414
use rustc_session::{declare_tool_lint, impl_lint_pass};
15-
use rustc_span::source_map::{Span, Spanned};
15+
use rustc_span::source_map::Span;
1616
use std::cmp::Ordering;
1717

1818
declare_clippy_lint! {
@@ -498,35 +498,30 @@ fn check_reversed_empty_range(cx: &LateContext<'_>, expr: &Expr<'_>) {
498498
}
499499

500500
fn y_plus_one<'t>(cx: &LateContext<'_>, expr: &'t Expr<'_>) -> Option<&'t Expr<'t>> {
501-
match expr.kind {
502-
ExprKind::Binary(
503-
Spanned {
504-
node: BinOpKind::Add, ..
505-
},
506-
lhs,
507-
rhs,
508-
) => {
509-
if is_integer_const(cx, lhs, 1) {
510-
Some(rhs)
511-
} else if is_integer_const(cx, rhs, 1) {
512-
Some(lhs)
513-
} else {
514-
None
515-
}
516-
},
517-
_ => None,
501+
if let ExprKind::Binary(node, lhs, rhs) = expr.kind
502+
&& matches!(node.node, BinOpKind::Add)
503+
&& lhs.span.ctxt() == rhs.span.ctxt()
504+
{
505+
if is_integer_const(cx, lhs, 1) {
506+
Some(rhs)
507+
} else if is_integer_const(cx, rhs, 1) {
508+
Some(lhs)
509+
} else {
510+
None
511+
}
512+
} else {
513+
None
518514
}
519515
}
520516

521517
fn y_minus_one<'t>(cx: &LateContext<'_>, expr: &'t Expr<'_>) -> Option<&'t Expr<'t>> {
522-
match expr.kind {
523-
ExprKind::Binary(
524-
Spanned {
525-
node: BinOpKind::Sub, ..
526-
},
527-
lhs,
528-
rhs,
529-
) if is_integer_const(cx, rhs, 1) => Some(lhs),
530-
_ => None,
518+
if let ExprKind::Binary(node, lhs, rhs) = expr.kind
519+
&& matches!(node.node, BinOpKind::Sub)
520+
&& lhs.span.ctxt() == rhs.span.ctxt()
521+
&& is_integer_const(cx, rhs, 1)
522+
{
523+
Some(lhs)
524+
} else {
525+
None
531526
}
532527
}

src/tools/clippy/clippy_lints/src/strings.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_sugg};
22
use clippy_utils::source::{snippet, snippet_with_applicability};
33
use clippy_utils::ty::is_type_diagnostic_item;
4-
use clippy_utils::{get_parent_expr, is_lint_allowed, match_function_call, method_calls, paths};
4+
use clippy_utils::{get_parent_expr, is_lint_allowed, is_qpath_lang_item, match_function_call, method_calls, paths};
55
use clippy_utils::{peel_blocks, SpanlessEq};
66
use if_chain::if_chain;
77
use rustc_errors::Applicability;
88
use rustc_hir::def_id::DefId;
9-
use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, LangItem, QPath};
9+
use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, LangItem};
1010
use rustc_lint::{LateContext, LateLintPass, LintContext};
1111
use rustc_middle::lint::in_external_macro;
1212
use rustc_middle::ty;
@@ -266,7 +266,8 @@ impl<'tcx> LateLintPass<'tcx> for StringLitAsBytes {
266266
if method_names[0] == sym!(as_bytes);
267267

268268
// Check for slicer
269-
if let ExprKind::Struct(QPath::LangItem(LangItem::Range, ..), _, _) = right.kind;
269+
if let ExprKind::Struct(ref qpath, _, _) = right.kind;
270+
if is_qpath_lang_item(cx, qpath, LangItem::Range);
270271

271272
then {
272273
let mut applicability = Applicability::MachineApplicable;

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

-1
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,6 @@ impl Types {
534534
}
535535
}
536536
},
537-
QPath::LangItem(..) => {},
538537
}
539538
},
540539
TyKind::Rptr(lt, ref mut_ty) => {

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

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
2-
use clippy_utils::{is_trait_method, is_try, match_trait_method, paths};
2+
use clippy_utils::{is_path_lang_item, is_trait_method, is_try, match_trait_method, paths};
33
use rustc_hir as hir;
44
use rustc_lint::{LateContext, LateLintPass};
55
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -54,10 +54,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedIoAmount {
5454
match expr.kind {
5555
hir::ExprKind::Match(res, _, _) if is_try(cx, expr).is_some() => {
5656
if let hir::ExprKind::Call(func, [ref arg_0, ..]) = res.kind {
57-
if matches!(
58-
func.kind,
59-
hir::ExprKind::Path(hir::QPath::LangItem(hir::LangItem::TryTraitBranch, ..))
60-
) {
57+
if is_path_lang_item(cx, func, hir::LangItem::TryTraitBranch) {
6158
check_map_error(cx, arg_0, expr);
6259
}
6360
} else {
@@ -77,13 +74,10 @@ impl<'tcx> LateLintPass<'tcx> for UnusedIoAmount {
7774

7875
/// If `expr` is an (e).await, return the inner expression "e" that's being
7976
/// waited on. Otherwise return None.
80-
fn try_remove_await<'a>(expr: &'a hir::Expr<'a>) -> Option<&hir::Expr<'a>> {
77+
fn try_remove_await<'a>(cx: &LateContext<'_>, expr: &'a hir::Expr<'a>) -> Option<&'a hir::Expr<'a>> {
8178
if let hir::ExprKind::Match(expr, _, hir::MatchSource::AwaitDesugar) = expr.kind {
8279
if let hir::ExprKind::Call(func, [ref arg_0, ..]) = expr.kind {
83-
if matches!(
84-
func.kind,
85-
hir::ExprKind::Path(hir::QPath::LangItem(hir::LangItem::IntoFutureIntoFuture, ..))
86-
) {
80+
if is_path_lang_item(cx, func, hir::LangItem::IntoFutureIntoFuture) {
8781
return Some(arg_0);
8882
}
8983
}
@@ -102,7 +96,7 @@ fn check_map_error(cx: &LateContext<'_>, call: &hir::Expr<'_>, expr: &hir::Expr<
10296
}
10397
}
10498

105-
if let Some(call) = try_remove_await(call) {
99+
if let Some(call) = try_remove_await(cx, call) {
106100
check_method_call(cx, call, expr, true);
107101
} else {
108102
check_method_call(cx, call, expr, false);

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

+1-6
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,7 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
266266
}
267267

268268
fn qpath(&self, qpath: &Binding<&QPath<'_>>) {
269-
if let QPath::LangItem(lang_item, ..) = *qpath.value {
270-
chain!(self, "matches!({qpath}, QPath::LangItem(LangItem::{lang_item:?}, _))");
271-
} else {
272-
chain!(self, "match_qpath({qpath}, &[{}])", path_to_string(qpath.value));
273-
}
269+
chain!(self, "match_qpath({qpath}, &[{}])", path_to_string(qpath.value));
274270
}
275271

276272
fn lit(&self, lit: &Binding<&Lit>) {
@@ -748,7 +744,6 @@ fn path_to_string(path: &QPath<'_>) -> String {
748744
},
749745
other => write!(s, "/* unimplemented: {other:?}*/").unwrap(),
750746
},
751-
QPath::LangItem(..) => panic!("path_to_string: called for lang item qpath"),
752747
}
753748
}
754749
let mut s = String::new();

src/tools/clippy/clippy_utils/src/check_proc_macro.rs

-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ fn qpath_search_pat(path: &QPath<'_>) -> (Pat, Pat) {
103103
(start, end)
104104
},
105105
QPath::TypeRelative(_, name) => (Pat::Str(""), Pat::Sym(name.ident.name)),
106-
QPath::LangItem(..) => (Pat::Str(""), Pat::Str("")),
107106
}
108107
}
109108

src/tools/clippy/clippy_utils/src/eager_or_lazy.rs

-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ fn expr_eagerness<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> EagernessS
138138
QPath::TypeRelative(_, name) => {
139139
self.eagerness |= fn_eagerness(self.cx, id, name.ident.name, !args.is_empty());
140140
},
141-
QPath::LangItem(..) => self.eagerness = Lazy,
142141
},
143142
_ => self.eagerness = Lazy,
144143
},

0 commit comments

Comments
 (0)