Skip to content

Commit db074a0

Browse files
committed
Auto merge of #140540 - flip1995:clippy-subtree-update, r=Manishearth
Clippy subtree update r? `@Manishearth`
2 parents cb0d6e7 + c9992d6 commit db074a0

File tree

109 files changed

+1819
-582
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+1819
-582
lines changed

src/tools/clippy/clippy_lints/src/assigning_clones.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::mir::{PossibleBorrowerMap, enclosing_mir};
44
use clippy_utils::msrvs::{self, Msrv};
55
use clippy_utils::sugg::Sugg;
6-
use clippy_utils::{is_diag_trait_item, is_in_test, last_path_segment, local_is_initialized, path_to_local};
6+
use clippy_utils::{is_diag_trait_item, is_in_test, last_path_segment, local_is_initialized, path_to_local, sym};
77
use rustc_errors::Applicability;
88
use rustc_hir::{self as hir, Expr, ExprKind};
99
use rustc_lint::{LateContext, LateLintPass};
1010
use rustc_middle::mir;
1111
use rustc_middle::ty::{self, Instance, Mutability};
1212
use rustc_session::impl_lint_pass;
13-
use rustc_span::symbol::sym;
1413
use rustc_span::{Span, SyntaxContext};
1514

1615
declare_clippy_lint! {
@@ -86,9 +85,9 @@ impl<'tcx> LateLintPass<'tcx> for AssigningClones {
8685
&& ctxt.is_root()
8786
&& let which_trait = match fn_name {
8887
sym::clone if is_diag_trait_item(cx, fn_id, sym::Clone) => CloneTrait::Clone,
89-
_ if fn_name.as_str() == "to_owned"
90-
&& is_diag_trait_item(cx, fn_id, sym::ToOwned)
91-
&& self.msrv.meets(cx, msrvs::CLONE_INTO) =>
88+
sym::to_owned
89+
if is_diag_trait_item(cx, fn_id, sym::ToOwned)
90+
&& self.msrv.meets(cx, msrvs::CLONE_INTO) =>
9291
{
9392
CloneTrait::ToOwned
9493
},
@@ -112,7 +111,7 @@ impl<'tcx> LateLintPass<'tcx> for AssigningClones {
112111
&& resolved_assoc_items.in_definition_order().any(|assoc|
113112
match which_trait {
114113
CloneTrait::Clone => assoc.name() == sym::clone_from,
115-
CloneTrait::ToOwned => assoc.name().as_str() == "clone_into",
114+
CloneTrait::ToOwned => assoc.name() == sym::clone_into,
116115
}
117116
)
118117
&& !clone_source_borrows_from_dest(cx, lhs, rhs.span)

src/tools/clippy/clippy_lints/src/attrs/blanket_clippy_restriction_lints.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
use super::BLANKET_CLIPPY_RESTRICTION_LINTS;
22
use super::utils::extract_clippy_lint;
33
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_then};
4+
use clippy_utils::sym;
45
use rustc_ast::MetaItemInner;
56
use rustc_lint::{EarlyContext, Level, LintContext};
7+
use rustc_span::DUMMY_SP;
68
use rustc_span::symbol::Symbol;
7-
use rustc_span::{DUMMY_SP, sym};
89

910
pub(super) fn check(cx: &EarlyContext<'_>, name: Symbol, items: &[MetaItemInner]) {
1011
for lint in items {
11-
if let Some(lint_name) = extract_clippy_lint(lint)
12-
&& lint_name.as_str() == "restriction"
13-
&& name != sym::allow
14-
{
12+
if name != sym::allow && extract_clippy_lint(lint) == Some(sym::restriction) {
1513
span_lint_and_help(
1614
cx,
1715
BLANKET_CLIPPY_RESTRICTION_LINTS,

src/tools/clippy/clippy_lints/src/attrs/deprecated_cfg_attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ fn check_deprecated_cfg_recursively(cx: &EarlyContext<'_>, attr: &rustc_ast::Met
7373
}
7474

7575
fn check_cargo_clippy_attr(cx: &EarlyContext<'_>, item: &rustc_ast::MetaItem) {
76-
if item.has_name(sym::feature) && item.value_str().is_some_and(|v| v.as_str() == "cargo-clippy") {
76+
if item.has_name(sym::feature) && item.value_str() == Some(sym::cargo_clippy) {
7777
span_lint_and_sugg(
7878
cx,
7979
DEPRECATED_CLIPPY_CFG_ATTR,

src/tools/clippy/clippy_lints/src/attrs/deprecated_semver.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use super::DEPRECATED_SEMVER;
22
use clippy_utils::diagnostics::span_lint;
3+
use clippy_utils::sym;
34
use rustc_ast::{LitKind, MetaItemLit};
45
use rustc_lint::EarlyContext;
56
use rustc_span::Span;
67
use semver::Version;
78

89
pub(super) fn check(cx: &EarlyContext<'_>, span: Span, lit: &MetaItemLit) {
910
if let LitKind::Str(is, _) = lit.kind
10-
&& (is.as_str() == "TBD" || Version::parse(is.as_str()).is_ok())
11+
&& (is == sym::TBD || Version::parse(is.as_str()).is_ok())
1112
{
1213
return;
1314
}

src/tools/clippy/clippy_lints/src/casts/cast_abs_to_unsigned.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::msrvs::{self, Msrv};
33
use clippy_utils::sugg::Sugg;
4+
use clippy_utils::sym;
45
use rustc_errors::Applicability;
56
use rustc_hir::{Expr, ExprKind};
67
use rustc_lint::LateContext;
@@ -19,7 +20,7 @@ pub(super) fn check(
1920
if let ty::Int(from) = cast_from.kind()
2021
&& let ty::Uint(to) = cast_to.kind()
2122
&& let ExprKind::MethodCall(method_path, receiver, [], _) = cast_expr.kind
22-
&& method_path.ident.name.as_str() == "abs"
23+
&& method_path.ident.name == sym::abs
2324
&& msrv.meets(cx, msrvs::UNSIGNED_ABS)
2425
{
2526
let span = if from.bit_width() == to.bit_width() {

src/tools/clippy/clippy_lints/src/casts/cast_possible_truncation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use clippy_utils::consts::{ConstEvalCtxt, Constant};
22
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
3-
use clippy_utils::expr_or_init;
43
use clippy_utils::source::snippet;
54
use clippy_utils::sugg::Sugg;
65
use clippy_utils::ty::{get_discriminant_value, is_isize_or_usize};
6+
use clippy_utils::{expr_or_init, sym};
77
use rustc_abi::IntegerType;
88
use rustc_errors::{Applicability, Diag};
99
use rustc_hir::def::{DefKind, Res};
@@ -73,7 +73,7 @@ fn apply_reductions(cx: &LateContext<'_>, nbits: u64, expr: &Expr<'_>, signed: b
7373
nbits
7474
},
7575
ExprKind::MethodCall(method, _value, [], _) => {
76-
if method.ident.name.as_str() == "signum" {
76+
if method.ident.name == sym::signum {
7777
0 // do not lint if cast comes from a `signum` function
7878
} else {
7979
nbits

src/tools/clippy/clippy_lints/src/casts/cast_ptr_alignment.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use clippy_utils::diagnostics::span_lint;
22
use clippy_utils::ty::is_c_void;
3-
use clippy_utils::{get_parent_expr, is_hir_ty_cfg_dependant};
3+
use clippy_utils::{get_parent_expr, is_hir_ty_cfg_dependant, sym};
44
use rustc_hir::{Expr, ExprKind, GenericArg};
55
use rustc_lint::LateContext;
66
use rustc_middle::ty::layout::LayoutOf;
77
use rustc_middle::ty::{self, Ty};
8-
use rustc_span::sym;
98

109
use super::CAST_PTR_ALIGNMENT;
1110

@@ -20,7 +19,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
2019
);
2120
lint_cast_ptr_alignment(cx, expr, cast_from, cast_to);
2221
} else if let ExprKind::MethodCall(method_path, self_arg, [], _) = &expr.kind
23-
&& method_path.ident.name.as_str() == "cast"
22+
&& method_path.ident.name == sym::cast
2423
&& let Some(generic_args) = method_path.args
2524
&& let [GenericArg::Type(cast_to)] = generic_args.args
2625
// There probably is no obvious reason to do this, just to be consistent with `as` cases.

src/tools/clippy/clippy_lints/src/casts/unnecessary_cast.rs

+42-13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use rustc_errors::Applicability;
88
use rustc_hir::def::{DefKind, Res};
99
use rustc_hir::{Expr, ExprKind, Lit, Node, Path, QPath, TyKind, UnOp};
1010
use rustc_lint::{LateContext, LintContext};
11+
use rustc_middle::ty::adjustment::Adjust;
1112
use rustc_middle::ty::{self, FloatTy, InferTy, Ty};
13+
use rustc_span::{Symbol, sym};
1214
use std::ops::ControlFlow;
1315

1416
use super::UNNECESSARY_CAST;
@@ -142,6 +144,33 @@ pub(super) fn check<'tcx>(
142144
}
143145

144146
if cast_from.kind() == cast_to.kind() && !expr.span.in_external_macro(cx.sess().source_map()) {
147+
enum MaybeParenOrBlock {
148+
Paren,
149+
Block,
150+
Nothing,
151+
}
152+
153+
fn is_borrow_expr(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
154+
matches!(expr.kind, ExprKind::AddrOf(..))
155+
|| cx
156+
.typeck_results()
157+
.expr_adjustments(expr)
158+
.first()
159+
.is_some_and(|adj| matches!(adj.kind, Adjust::Borrow(_)))
160+
}
161+
162+
fn is_in_allowed_macro(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
163+
const ALLOWED_MACROS: &[Symbol] = &[
164+
sym::format_args_macro,
165+
sym::assert_eq_macro,
166+
sym::debug_assert_eq_macro,
167+
sym::assert_ne_macro,
168+
sym::debug_assert_ne_macro,
169+
];
170+
matches!(expr.span.ctxt().outer_expn_data().macro_def_id, Some(def_id) if
171+
cx.tcx.get_diagnostic_name(def_id).is_some_and(|sym| ALLOWED_MACROS.contains(&sym)))
172+
}
173+
145174
if let Some(id) = path_to_local(cast_expr)
146175
&& !cx.tcx.hir_span(id).eq_ctxt(cast_expr.span)
147176
{
@@ -150,26 +179,26 @@ pub(super) fn check<'tcx>(
150179
return false;
151180
}
152181

153-
// If the whole cast expression is a unary expression (`(*x as T)`) or an addressof
154-
// expression (`(&x as T)`), then not surrounding the suggestion into a block risks us
155-
// changing the precedence of operators if the cast expression is followed by an operation
156-
// with higher precedence than the unary operator (`(*x as T).foo()` would become
157-
// `*x.foo()`, which changes what the `*` applies on).
158-
// The same is true if the expression encompassing the cast expression is a unary
159-
// expression or an addressof expression.
160-
let needs_block = matches!(cast_expr.kind, ExprKind::Unary(..) | ExprKind::AddrOf(..))
161-
|| get_parent_expr(cx, expr).is_some_and(|e| matches!(e.kind, ExprKind::Unary(..) | ExprKind::AddrOf(..)));
182+
// Changing `&(x as i32)` to `&x` would change the meaning of the code because the previous creates
183+
// a reference to the temporary while the latter creates a reference to the original value.
184+
let surrounding = match cx.tcx.parent_hir_node(expr.hir_id) {
185+
Node::Expr(parent) if is_borrow_expr(cx, parent) && !is_in_allowed_macro(cx, parent) => {
186+
MaybeParenOrBlock::Block
187+
},
188+
Node::Expr(parent) if cast_expr.precedence() < parent.precedence() => MaybeParenOrBlock::Paren,
189+
_ => MaybeParenOrBlock::Nothing,
190+
};
162191

163192
span_lint_and_sugg(
164193
cx,
165194
UNNECESSARY_CAST,
166195
expr.span,
167196
format!("casting to the same type is unnecessary (`{cast_from}` -> `{cast_to}`)"),
168197
"try",
169-
if needs_block {
170-
format!("{{ {cast_str} }}")
171-
} else {
172-
cast_str
198+
match surrounding {
199+
MaybeParenOrBlock::Paren => format!("({cast_str})"),
200+
MaybeParenOrBlock::Block => format!("{{ {cast_str} }}"),
201+
MaybeParenOrBlock::Nothing => cast_str,
173202
},
174203
Applicability::MachineApplicable,
175204
);

src/tools/clippy/clippy_lints/src/crate_in_macro_def.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use rustc_ast::tokenstream::{TokenStream, TokenTree};
55
use rustc_errors::Applicability;
66
use rustc_lint::{EarlyContext, EarlyLintPass};
77
use rustc_session::declare_lint_pass;
8-
use rustc_span::Span;
98
use rustc_span::symbol::sym;
9+
use rustc_span::{Span, kw};
1010

1111
declare_clippy_lint! {
1212
/// ### What it does
@@ -105,12 +105,11 @@ fn contains_unhygienic_crate_reference(tts: &TokenStream) -> Option<Span> {
105105
fn is_crate_keyword(tt: &TokenTree) -> Option<Span> {
106106
if let TokenTree::Token(
107107
Token {
108-
kind: TokenKind::Ident(symbol, _),
108+
kind: TokenKind::Ident(kw::Crate, _),
109109
span,
110110
},
111111
_,
112112
) = tt
113-
&& symbol.as_str() == "crate"
114113
{
115114
Some(*span)
116115
} else {

src/tools/clippy/clippy_lints/src/equatable_if_let.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,38 @@ fn is_structural_partial_eq<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, other: T
6868
}
6969
}
7070

71+
/// Check if the pattern has any type mismatch that would prevent it from being used in an equality
72+
/// check. This can happen if the expr has a reference type and the corresponding pattern is a
73+
/// literal.
74+
fn contains_type_mismatch(cx: &LateContext<'_>, pat: &Pat<'_>) -> bool {
75+
let mut result = false;
76+
pat.walk(|p| {
77+
if result {
78+
return false;
79+
}
80+
81+
if p.span.in_external_macro(cx.sess().source_map()) {
82+
return true;
83+
}
84+
85+
let adjust_pat = match p.kind {
86+
PatKind::Or([p, ..]) => p,
87+
_ => p,
88+
};
89+
90+
if let Some(adjustments) = cx.typeck_results().pat_adjustments().get(adjust_pat.hir_id)
91+
&& adjustments.first().is_some_and(|first| first.source.is_ref())
92+
{
93+
result = true;
94+
return false;
95+
}
96+
97+
true
98+
});
99+
100+
result
101+
}
102+
71103
impl<'tcx> LateLintPass<'tcx> for PatternEquality {
72104
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
73105
if let ExprKind::Let(let_expr) = expr.kind
@@ -78,7 +110,7 @@ impl<'tcx> LateLintPass<'tcx> for PatternEquality {
78110
let pat_ty = cx.typeck_results().pat_ty(let_expr.pat);
79111
let mut applicability = Applicability::MachineApplicable;
80112

81-
if is_structural_partial_eq(cx, exp_ty, pat_ty) {
113+
if is_structural_partial_eq(cx, exp_ty, pat_ty) && !contains_type_mismatch(cx, let_expr.pat) {
82114
let pat_str = match let_expr.pat.kind {
83115
PatKind::Struct(..) => format!(
84116
"({})",

src/tools/clippy/clippy_lints/src/floating_point_arithmetic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::consts::{ConstEvalCtxt, Constant};
33
use clippy_utils::diagnostics::span_lint_and_sugg;
44
use clippy_utils::{
55
eq_expr_value, get_parent_expr, higher, is_in_const_context, is_inherent_method_call, is_no_std_crate,
6-
numeric_literal, peel_blocks, sugg,
6+
numeric_literal, peel_blocks, sugg, sym,
77
};
88
use rustc_errors::Applicability;
99
use rustc_hir::{BinOpKind, Expr, ExprKind, PathSegment, UnOp};
@@ -435,7 +435,7 @@ fn check_expm1(cx: &LateContext<'_>, expr: &Expr<'_>) {
435435
rhs,
436436
) = expr.kind
437437
&& let ExprKind::MethodCall(path, self_arg, [], _) = &lhs.kind
438-
&& path.ident.name.as_str() == "exp"
438+
&& path.ident.name == sym::exp
439439
&& cx.typeck_results().expr_ty(lhs).is_floating_point()
440440
&& let Some(value) = ConstEvalCtxt::new(cx).eval(rhs)
441441
&& (F32(1.0) == value || F64(1.0) == value)

src/tools/clippy/clippy_lints/src/from_raw_with_void_ptr.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::path_def_id;
32
use clippy_utils::ty::is_c_void;
3+
use clippy_utils::{path_def_id, sym};
44
use rustc_hir::def_id::DefId;
55
use rustc_hir::{Expr, ExprKind, QPath};
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_middle::ty;
88
use rustc_session::declare_lint_pass;
9-
use rustc_span::sym;
109

1110
declare_clippy_lint! {
1211
/// ### What it does
@@ -41,7 +40,7 @@ impl LateLintPass<'_> for FromRawWithVoidPtr {
4140
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
4241
if let ExprKind::Call(box_from_raw, [arg]) = expr.kind
4342
&& let ExprKind::Path(QPath::TypeRelative(ty, seg)) = box_from_raw.kind
44-
&& seg.ident.name.as_str() == "from_raw"
43+
&& seg.ident.name == sym::from_raw
4544
&& let Some(type_str) = path_def_id(cx, ty).and_then(|id| def_id_matches_type(cx, id))
4645
&& let arg_kind = cx.typeck_results().expr_ty(arg).kind()
4746
&& let ty::RawPtr(ty, _) = arg_kind

src/tools/clippy/clippy_lints/src/from_str_radix_10.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::sugg::Sugg;
33
use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item};
4-
use clippy_utils::{is_in_const_context, is_integer_literal};
4+
use clippy_utils::{is_in_const_context, is_integer_literal, sym};
55
use rustc_errors::Applicability;
66
use rustc_hir::{Expr, ExprKind, LangItem, PrimTy, QPath, TyKind, def};
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_middle::ty::Ty;
99
use rustc_session::declare_lint_pass;
10-
use rustc_span::symbol::sym;
1110

1211
declare_clippy_lint! {
1312
/// ### What it does
@@ -53,7 +52,7 @@ impl<'tcx> LateLintPass<'tcx> for FromStrRadix10 {
5352

5453
// check if the second part of the path indeed calls the associated
5554
// function `from_str_radix`
56-
&& pathseg.ident.name.as_str() == "from_str_radix"
55+
&& pathseg.ident.name == sym::from_str_radix
5756

5857
// check if the first part of the path is some integer primitive
5958
&& let TyKind::Path(ty_qpath) = &ty.kind

0 commit comments

Comments
 (0)