Skip to content

Commit 4379767

Browse files
committed
Merge remote-tracking branch 'upstream/master' into rustup
2 parents 6d58910 + 549107d commit 4379767

File tree

118 files changed

+1874
-602
lines changed

Some content is hidden

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

118 files changed

+1874
-602
lines changed

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,10 @@ harness = false
7373
[[test]]
7474
name = "dogfood"
7575
harness = false
76+
77+
# quine-mc_cluskey makes up a significant part of the runtime in dogfood
78+
# due to the number of conditions in the clippy_lints crate
79+
# and enabling optimizations for that specific dependency helps a bit
80+
# without increasing total build times.
81+
[profile.dev.package.quine-mc_cluskey]
82+
opt-level = 3

clippy_lints/src/assigning_clones.rs

Lines changed: 5 additions & 6 deletions
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)

clippy_lints/src/attrs/blanket_clippy_restriction_lints.rs

Lines changed: 3 additions & 5 deletions
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,

clippy_lints/src/attrs/deprecated_cfg_attr.rs

Lines changed: 1 addition & 1 deletion
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,

clippy_lints/src/attrs/deprecated_semver.rs

Lines changed: 2 additions & 1 deletion
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
}

clippy_lints/src/casts/cast_abs_to_unsigned.rs

Lines changed: 2 additions & 1 deletion
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() {

clippy_lints/src/casts/cast_possible_truncation.rs

Lines changed: 2 additions & 2 deletions
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

clippy_lints/src/casts/cast_ptr_alignment.rs

Lines changed: 2 additions & 3 deletions
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.

clippy_lints/src/casts/unnecessary_cast.rs

Lines changed: 42 additions & 13 deletions
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
);

clippy_lints/src/crate_in_macro_def.rs

Lines changed: 2 additions & 3 deletions
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 {

0 commit comments

Comments
 (0)