Skip to content

Clippy subtree update #140540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
cbfe1f5
Restrict the cases where `ptr_eq` triggers
samueltardieu Apr 2, 2025
2422f0b
Restrict the cases where `ptr_eq` triggers (#14526)
Alexendoo Apr 22, 2025
ff428d9
Merge commit '0621446356e20fd2ead13a6763bb936c95eb0cfa' into clippy-s…
flip1995 Apr 22, 2025
bf713a0
style: pull one more `if` into the let-chain
ada4a Apr 22, 2025
2304a9c
remove non-existent pathspec from pre-commit hook
ada4a Apr 22, 2025
aa27ae3
remove non-existent pathspec from pre-commit hook (#14671)
Manishearth Apr 22, 2025
dd5948c
Clippy: Fix doc issue
flip1995 Apr 23, 2025
7c5312b
Reword `needless_question_mark` diagnostics and docs
Alexendoo Apr 23, 2025
dc695f5
Reword `needless_question_mark` diagnostics and docs (#14682)
Manishearth Apr 23, 2025
34f81f9
style: pull one more `if` into the let-chain (#14669)
y21 Apr 23, 2025
6e64338
Suggest {to,from}_ne_bytes for transmutations between arrays and inte…
bend-n Mar 31, 2025
6ee75c4
Remove `weak` alias terminology
BoxyUwU Apr 24, 2025
736be8b
Consistently refer to the `?` operator
Alexendoo Apr 24, 2025
fc12b5b
fix-issue-14665
Kivooeo Apr 22, 2025
91ed606
Consistently refer to the `?` operator (#14687)
y21 Apr 24, 2025
7b337f6
Replace some `Symbol::as_str` usage
Alexendoo Apr 23, 2025
8a9153d
Merge from rustc
Apr 25, 2025
148c9a1
Fix error message for static references or mutable references
yuk1ty Apr 20, 2025
3f72ffa
fix: `unnecessary_cast` suggests extra brackets when in macro
profetia Apr 17, 2025
ad69347
fix: `equatable_if_let` suggests wrongly when involving reference
profetia Mar 30, 2025
ff307ba
fix: `unnecessary_cast` suggests extra brackets when in macro (#14643)
Jarcho Apr 26, 2025
58cfdb7
fix: `equatable_if_let` suggests wrongly when involving reference (#1…
dswij Apr 26, 2025
39a4086
`manual_div_ceil`: fix suggestions when macro is involved (#14666)
dswij Apr 27, 2025
5d8fb77
fix: `unused_unit` suggests wrongly when unit never type fallback
profetia Apr 17, 2025
5123ad5
Fix `zombie_processes` FP inside closures
profetia Apr 27, 2025
0dd9722
Replace some `Symbol::as_str` usage (#14679)
y21 Apr 27, 2025
542762e
fix: `unused_unit` suggests wrongly when unit never type fallback (#1…
Jarcho Apr 27, 2025
bca637c
Add or-patterns to pattern types
oli-obk Feb 27, 2025
92ba063
Rollup merge of #140249 - BoxyUwU:remove_weak_alias_terminology, r=ol…
GuillaumeGomez Apr 28, 2025
549107d
Fix `zombie_processes` FP inside closures (#14696)
Manishearth Apr 28, 2025
d172204
Merge from rustc
Apr 29, 2025
6d58910
Rollup merge of #139909 - oli-obk:or-patterns, r=BoxyUwU
tgross35 Apr 29, 2025
4379767
Merge remote-tracking branch 'upstream/master' into rustup
flip1995 May 1, 2025
8a91bbf
Bump nightly version -> 2025-05-01
flip1995 May 1, 2025
03a5b6b
Rustup (#14721)
flip1995 May 1, 2025
c9992d6
Merge commit '03a5b6b976ac121f4233775c49a4bce026065b47' into clippy-s…
flip1995 May 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/tools/clippy/clippy_lints/src/assigning_clones.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::mir::{PossibleBorrowerMap, enclosing_mir};
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::sugg::Sugg;
use clippy_utils::{is_diag_trait_item, is_in_test, last_path_segment, local_is_initialized, path_to_local};
use clippy_utils::{is_diag_trait_item, is_in_test, last_path_segment, local_is_initialized, path_to_local, sym};
use rustc_errors::Applicability;
use rustc_hir::{self as hir, Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::mir;
use rustc_middle::ty::{self, Instance, Mutability};
use rustc_session::impl_lint_pass;
use rustc_span::symbol::sym;
use rustc_span::{Span, SyntaxContext};

declare_clippy_lint! {
Expand Down Expand Up @@ -86,9 +85,9 @@ impl<'tcx> LateLintPass<'tcx> for AssigningClones {
&& ctxt.is_root()
&& let which_trait = match fn_name {
sym::clone if is_diag_trait_item(cx, fn_id, sym::Clone) => CloneTrait::Clone,
_ if fn_name.as_str() == "to_owned"
&& is_diag_trait_item(cx, fn_id, sym::ToOwned)
&& self.msrv.meets(cx, msrvs::CLONE_INTO) =>
sym::to_owned
if is_diag_trait_item(cx, fn_id, sym::ToOwned)
&& self.msrv.meets(cx, msrvs::CLONE_INTO) =>
{
CloneTrait::ToOwned
},
Expand All @@ -112,7 +111,7 @@ impl<'tcx> LateLintPass<'tcx> for AssigningClones {
&& resolved_assoc_items.in_definition_order().any(|assoc|
match which_trait {
CloneTrait::Clone => assoc.name() == sym::clone_from,
CloneTrait::ToOwned => assoc.name().as_str() == "clone_into",
CloneTrait::ToOwned => assoc.name() == sym::clone_into,
}
)
&& !clone_source_borrows_from_dest(cx, lhs, rhs.span)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use super::BLANKET_CLIPPY_RESTRICTION_LINTS;
use super::utils::extract_clippy_lint;
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_then};
use clippy_utils::sym;
use rustc_ast::MetaItemInner;
use rustc_lint::{EarlyContext, Level, LintContext};
use rustc_span::DUMMY_SP;
use rustc_span::symbol::Symbol;
use rustc_span::{DUMMY_SP, sym};

pub(super) fn check(cx: &EarlyContext<'_>, name: Symbol, items: &[MetaItemInner]) {
for lint in items {
if let Some(lint_name) = extract_clippy_lint(lint)
&& lint_name.as_str() == "restriction"
&& name != sym::allow
{
if name != sym::allow && extract_clippy_lint(lint) == Some(sym::restriction) {
span_lint_and_help(
cx,
BLANKET_CLIPPY_RESTRICTION_LINTS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn check_deprecated_cfg_recursively(cx: &EarlyContext<'_>, attr: &rustc_ast::Met
}

fn check_cargo_clippy_attr(cx: &EarlyContext<'_>, item: &rustc_ast::MetaItem) {
if item.has_name(sym::feature) && item.value_str().is_some_and(|v| v.as_str() == "cargo-clippy") {
if item.has_name(sym::feature) && item.value_str() == Some(sym::cargo_clippy) {
span_lint_and_sugg(
cx,
DEPRECATED_CLIPPY_CFG_ATTR,
Expand Down
3 changes: 2 additions & 1 deletion src/tools/clippy/clippy_lints/src/attrs/deprecated_semver.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use super::DEPRECATED_SEMVER;
use clippy_utils::diagnostics::span_lint;
use clippy_utils::sym;
use rustc_ast::{LitKind, MetaItemLit};
use rustc_lint::EarlyContext;
use rustc_span::Span;
use semver::Version;

pub(super) fn check(cx: &EarlyContext<'_>, span: Span, lit: &MetaItemLit) {
if let LitKind::Str(is, _) = lit.kind
&& (is.as_str() == "TBD" || Version::parse(is.as_str()).is_ok())
&& (is == sym::TBD || Version::parse(is.as_str()).is_ok())
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::sugg::Sugg;
use clippy_utils::sym;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::LateContext;
Expand All @@ -19,7 +20,7 @@ pub(super) fn check(
if let ty::Int(from) = cast_from.kind()
&& let ty::Uint(to) = cast_to.kind()
&& let ExprKind::MethodCall(method_path, receiver, [], _) = cast_expr.kind
&& method_path.ident.name.as_str() == "abs"
&& method_path.ident.name == sym::abs
&& msrv.meets(cx, msrvs::UNSIGNED_ABS)
{
let span = if from.bit_width() == to.bit_width() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use clippy_utils::consts::{ConstEvalCtxt, Constant};
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
use clippy_utils::expr_or_init;
use clippy_utils::source::snippet;
use clippy_utils::sugg::Sugg;
use clippy_utils::ty::{get_discriminant_value, is_isize_or_usize};
use clippy_utils::{expr_or_init, sym};
use rustc_abi::IntegerType;
use rustc_errors::{Applicability, Diag};
use rustc_hir::def::{DefKind, Res};
Expand Down Expand Up @@ -73,7 +73,7 @@ fn apply_reductions(cx: &LateContext<'_>, nbits: u64, expr: &Expr<'_>, signed: b
nbits
},
ExprKind::MethodCall(method, _value, [], _) => {
if method.ident.name.as_str() == "signum" {
if method.ident.name == sym::signum {
0 // do not lint if cast comes from a `signum` function
} else {
nbits
Expand Down
5 changes: 2 additions & 3 deletions src/tools/clippy/clippy_lints/src/casts/cast_ptr_alignment.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use clippy_utils::diagnostics::span_lint;
use clippy_utils::ty::is_c_void;
use clippy_utils::{get_parent_expr, is_hir_ty_cfg_dependant};
use clippy_utils::{get_parent_expr, is_hir_ty_cfg_dependant, sym};
use rustc_hir::{Expr, ExprKind, GenericArg};
use rustc_lint::LateContext;
use rustc_middle::ty::layout::LayoutOf;
use rustc_middle::ty::{self, Ty};
use rustc_span::sym;

use super::CAST_PTR_ALIGNMENT;

Expand All @@ -20,7 +19,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
);
lint_cast_ptr_alignment(cx, expr, cast_from, cast_to);
} else if let ExprKind::MethodCall(method_path, self_arg, [], _) = &expr.kind
&& method_path.ident.name.as_str() == "cast"
&& method_path.ident.name == sym::cast
&& let Some(generic_args) = method_path.args
&& let [GenericArg::Type(cast_to)] = generic_args.args
// There probably is no obvious reason to do this, just to be consistent with `as` cases.
Expand Down
55 changes: 42 additions & 13 deletions src/tools/clippy/clippy_lints/src/casts/unnecessary_cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use rustc_errors::Applicability;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::{Expr, ExprKind, Lit, Node, Path, QPath, TyKind, UnOp};
use rustc_lint::{LateContext, LintContext};
use rustc_middle::ty::adjustment::Adjust;
use rustc_middle::ty::{self, FloatTy, InferTy, Ty};
use rustc_span::{Symbol, sym};
use std::ops::ControlFlow;

use super::UNNECESSARY_CAST;
Expand Down Expand Up @@ -142,6 +144,33 @@ pub(super) fn check<'tcx>(
}

if cast_from.kind() == cast_to.kind() && !expr.span.in_external_macro(cx.sess().source_map()) {
enum MaybeParenOrBlock {
Paren,
Block,
Nothing,
}

fn is_borrow_expr(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
matches!(expr.kind, ExprKind::AddrOf(..))
|| cx
.typeck_results()
.expr_adjustments(expr)
.first()
.is_some_and(|adj| matches!(adj.kind, Adjust::Borrow(_)))
}

fn is_in_allowed_macro(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
const ALLOWED_MACROS: &[Symbol] = &[
sym::format_args_macro,
sym::assert_eq_macro,
sym::debug_assert_eq_macro,
sym::assert_ne_macro,
sym::debug_assert_ne_macro,
];
matches!(expr.span.ctxt().outer_expn_data().macro_def_id, Some(def_id) if
cx.tcx.get_diagnostic_name(def_id).is_some_and(|sym| ALLOWED_MACROS.contains(&sym)))
}

if let Some(id) = path_to_local(cast_expr)
&& !cx.tcx.hir_span(id).eq_ctxt(cast_expr.span)
{
Expand All @@ -150,26 +179,26 @@ pub(super) fn check<'tcx>(
return false;
}

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

span_lint_and_sugg(
cx,
UNNECESSARY_CAST,
expr.span,
format!("casting to the same type is unnecessary (`{cast_from}` -> `{cast_to}`)"),
"try",
if needs_block {
format!("{{ {cast_str} }}")
} else {
cast_str
match surrounding {
MaybeParenOrBlock::Paren => format!("({cast_str})"),
MaybeParenOrBlock::Block => format!("{{ {cast_str} }}"),
MaybeParenOrBlock::Nothing => cast_str,
},
Applicability::MachineApplicable,
);
Expand Down
5 changes: 2 additions & 3 deletions src/tools/clippy/clippy_lints/src/crate_in_macro_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use rustc_ast::tokenstream::{TokenStream, TokenTree};
use rustc_errors::Applicability;
use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_session::declare_lint_pass;
use rustc_span::Span;
use rustc_span::symbol::sym;
use rustc_span::{Span, kw};

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -105,12 +105,11 @@ fn contains_unhygienic_crate_reference(tts: &TokenStream) -> Option<Span> {
fn is_crate_keyword(tt: &TokenTree) -> Option<Span> {
if let TokenTree::Token(
Token {
kind: TokenKind::Ident(symbol, _),
kind: TokenKind::Ident(kw::Crate, _),
span,
},
_,
) = tt
&& symbol.as_str() == "crate"
{
Some(*span)
} else {
Expand Down
34 changes: 33 additions & 1 deletion src/tools/clippy/clippy_lints/src/equatable_if_let.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,38 @@ fn is_structural_partial_eq<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, other: T
}
}

/// Check if the pattern has any type mismatch that would prevent it from being used in an equality
/// check. This can happen if the expr has a reference type and the corresponding pattern is a
/// literal.
fn contains_type_mismatch(cx: &LateContext<'_>, pat: &Pat<'_>) -> bool {
let mut result = false;
pat.walk(|p| {
if result {
return false;
}

if p.span.in_external_macro(cx.sess().source_map()) {
return true;
}

let adjust_pat = match p.kind {
PatKind::Or([p, ..]) => p,
_ => p,
};

if let Some(adjustments) = cx.typeck_results().pat_adjustments().get(adjust_pat.hir_id)
&& adjustments.first().is_some_and(|first| first.source.is_ref())
{
result = true;
return false;
}

true
});

result
}

impl<'tcx> LateLintPass<'tcx> for PatternEquality {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
if let ExprKind::Let(let_expr) = expr.kind
Expand All @@ -78,7 +110,7 @@ impl<'tcx> LateLintPass<'tcx> for PatternEquality {
let pat_ty = cx.typeck_results().pat_ty(let_expr.pat);
let mut applicability = Applicability::MachineApplicable;

if is_structural_partial_eq(cx, exp_ty, pat_ty) {
if is_structural_partial_eq(cx, exp_ty, pat_ty) && !contains_type_mismatch(cx, let_expr.pat) {
let pat_str = match let_expr.pat.kind {
PatKind::Struct(..) => format!(
"({})",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clippy_utils::consts::{ConstEvalCtxt, Constant};
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::{
eq_expr_value, get_parent_expr, higher, is_in_const_context, is_inherent_method_call, is_no_std_crate,
numeric_literal, peel_blocks, sugg,
numeric_literal, peel_blocks, sugg, sym,
};
use rustc_errors::Applicability;
use rustc_hir::{BinOpKind, Expr, ExprKind, PathSegment, UnOp};
Expand Down Expand Up @@ -435,7 +435,7 @@ fn check_expm1(cx: &LateContext<'_>, expr: &Expr<'_>) {
rhs,
) = expr.kind
&& let ExprKind::MethodCall(path, self_arg, [], _) = &lhs.kind
&& path.ident.name.as_str() == "exp"
&& path.ident.name == sym::exp
&& cx.typeck_results().expr_ty(lhs).is_floating_point()
&& let Some(value) = ConstEvalCtxt::new(cx).eval(rhs)
&& (F32(1.0) == value || F64(1.0) == value)
Expand Down
5 changes: 2 additions & 3 deletions src/tools/clippy/clippy_lints/src/from_raw_with_void_ptr.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::path_def_id;
use clippy_utils::ty::is_c_void;
use clippy_utils::{path_def_id, sym};
use rustc_hir::def_id::DefId;
use rustc_hir::{Expr, ExprKind, QPath};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty;
use rustc_session::declare_lint_pass;
use rustc_span::sym;

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -41,7 +40,7 @@ impl LateLintPass<'_> for FromRawWithVoidPtr {
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
if let ExprKind::Call(box_from_raw, [arg]) = expr.kind
&& let ExprKind::Path(QPath::TypeRelative(ty, seg)) = box_from_raw.kind
&& seg.ident.name.as_str() == "from_raw"
&& seg.ident.name == sym::from_raw
&& let Some(type_str) = path_def_id(cx, ty).and_then(|id| def_id_matches_type(cx, id))
&& let arg_kind = cx.typeck_results().expr_ty(arg).kind()
&& let ty::RawPtr(ty, _) = arg_kind
Expand Down
5 changes: 2 additions & 3 deletions src/tools/clippy/clippy_lints/src/from_str_radix_10.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::sugg::Sugg;
use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item};
use clippy_utils::{is_in_const_context, is_integer_literal};
use clippy_utils::{is_in_const_context, is_integer_literal, sym};
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind, LangItem, PrimTy, QPath, TyKind, def};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::Ty;
use rustc_session::declare_lint_pass;
use rustc_span::symbol::sym;

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -53,7 +52,7 @@ impl<'tcx> LateLintPass<'tcx> for FromStrRadix10 {

// check if the second part of the path indeed calls the associated
// function `from_str_radix`
&& pathseg.ident.name.as_str() == "from_str_radix"
&& pathseg.ident.name == sym::from_str_radix

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