Skip to content

Commit 9f96120

Browse files
authored
Rollup merge of rust-lang#134661 - dtolnay:prefixattr, r=fmease
Reduce precedence of expressions that have an outer attr Previously, `-Zunpretty=expanded` would expand this program as follows: ```rust #![feature(stmt_expr_attributes)] macro_rules! repro { ($e:expr) => { #[allow(deprecated)] $e }; } #[derive(Default)] struct Thing { #[deprecated] field: i32, } fn main() { let thing = Thing::default(); let _ = repro!(thing).field; } ``` ```rs #![feature(prelude_import)] #![feature(stmt_expr_attributes)] #[prelude_import] use std::prelude::rust_2021::*; #[macro_use] extern crate std; struct Thing { #[deprecated] field: i32, } #[automatically_derived] impl ::core::default::Default for Thing { #[inline] fn default() -> Thing { Thing { field: ::core::default::Default::default() } } } fn main() { let thing = Thing::default(); let _ = #[allow(deprecated)] thing.field; } ``` This is not the correct expansion. The correct output would have `(#[allow(deprecated)] thing).field` with the attribute applying only to `thing`, not to `thing.field`.
2 parents 252a6dd + 3d8d575 commit 9f96120

File tree

7 files changed

+11
-11
lines changed

7 files changed

+11
-11
lines changed

clippy_lints/src/casts/unnecessary_cast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ pub(super) fn check<'tcx>(
185185
Node::Expr(parent) if is_borrow_expr(cx, parent) && !is_in_allowed_macro(cx, parent) => {
186186
MaybeParenOrBlock::Block
187187
},
188-
Node::Expr(parent) if cast_expr.precedence() < parent.precedence() => MaybeParenOrBlock::Paren,
188+
Node::Expr(parent) if cx.precedence(cast_expr) < cx.precedence(parent) => MaybeParenOrBlock::Paren,
189189
_ => MaybeParenOrBlock::Nothing,
190190
};
191191

clippy_lints/src/dereference.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ fn report<'tcx>(
972972
"&"
973973
};
974974

975-
let expr_str = if !expr_is_macro_call && is_ufcs && expr.precedence() < ExprPrecedence::Prefix {
975+
let expr_str = if !expr_is_macro_call && is_ufcs && cx.precedence(expr) < ExprPrecedence::Prefix {
976976
Cow::Owned(format!("({expr_str})"))
977977
} else {
978978
expr_str
@@ -1015,10 +1015,10 @@ fn report<'tcx>(
10151015
Node::Expr(e) => match e.kind {
10161016
ExprKind::Call(callee, _) if callee.hir_id != data.first_expr.hir_id => false,
10171017
ExprKind::Call(..) => {
1018-
expr.precedence() < ExprPrecedence::Unambiguous
1018+
cx.precedence(expr) < ExprPrecedence::Unambiguous
10191019
|| matches!(expr.kind, ExprKind::Field(..))
10201020
},
1021-
_ => expr.precedence() < e.precedence(),
1021+
_ => cx.precedence(expr) < cx.precedence(e),
10221022
},
10231023
_ => false,
10241024
};
@@ -1066,7 +1066,7 @@ fn report<'tcx>(
10661066
Mutability::Not => "&",
10671067
Mutability::Mut => "&mut ",
10681068
};
1069-
(prefix, expr.precedence() < ExprPrecedence::Prefix)
1069+
(prefix, cx.precedence(expr) < ExprPrecedence::Prefix)
10701070
},
10711071
None if !ty.is_ref() && data.adjusted_ty.is_ref() => ("&", false),
10721072
_ => ("", false),
@@ -1172,7 +1172,7 @@ impl<'tcx> Dereferencing<'tcx> {
11721172
},
11731173
Some(parent) if !parent.span.from_expansion() => {
11741174
// Double reference might be needed at this point.
1175-
if parent.precedence() == ExprPrecedence::Unambiguous {
1175+
if cx.precedence(parent) == ExprPrecedence::Unambiguous {
11761176
// Parentheses would be needed here, don't lint.
11771177
*outer_pat = None;
11781178
} else {

clippy_lints/src/loops/single_element_loop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub(super) fn check<'tcx>(
8484
if !prefix.is_empty()
8585
&& (
8686
// Precedence of internal expression is less than or equal to precedence of `&expr`.
87-
arg_expression.precedence() <= ExprPrecedence::Prefix || is_range_literal(arg_expression)
87+
cx.precedence(arg_expression) <= ExprPrecedence::Prefix || is_range_literal(arg_expression)
8888
)
8989
{
9090
arg_snip = format!("({arg_snip})").into();

clippy_lints/src/matches/manual_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ where
117117
// it's being passed by value.
118118
let scrutinee = peel_hir_expr_refs(scrutinee).0;
119119
let (scrutinee_str, _) = snippet_with_context(cx, scrutinee.span, expr_ctxt, "..", &mut app);
120-
let scrutinee_str = if scrutinee.span.eq_ctxt(expr.span) && scrutinee.precedence() < ExprPrecedence::Unambiguous {
120+
let scrutinee_str = if scrutinee.span.eq_ctxt(expr.span) && cx.precedence(scrutinee) < ExprPrecedence::Unambiguous {
121121
format!("({scrutinee_str})")
122122
} else {
123123
scrutinee_str.into()

clippy_lints/src/neg_multiply.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ fn check_mul(cx: &LateContext<'_>, span: Span, lit: &Expr<'_>, exp: &Expr<'_>) {
6464
{
6565
let mut applicability = Applicability::MachineApplicable;
6666
let (snip, from_macro) = snippet_with_context(cx, exp.span, span.ctxt(), "..", &mut applicability);
67-
let suggestion = if !from_macro && exp.precedence() < ExprPrecedence::Prefix && !has_enclosing_paren(&snip) {
67+
let suggestion = if !from_macro && cx.precedence(exp) < ExprPrecedence::Prefix && !has_enclosing_paren(&snip) {
6868
format!("-({snip})")
6969
} else {
7070
format!("-{snip}")

clippy_lints/src/redundant_slicing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantSlicing {
8686
let (indexed_ty, indexed_ref_count) = peel_middle_ty_refs(cx.typeck_results().expr_ty(indexed));
8787
let parent_expr = get_parent_expr(cx, expr);
8888
let needs_parens_for_prefix =
89-
parent_expr.is_some_and(|parent| parent.precedence() > ExprPrecedence::Prefix);
89+
parent_expr.is_some_and(|parent| cx.precedence(parent) > ExprPrecedence::Prefix);
9090

9191
if expr_ty == indexed_ty {
9292
if expr_ref_count > indexed_ref_count {

clippy_lints/src/transmute/transmutes_expressible_as_ptr_casts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub(super) fn check<'tcx>(
4444
};
4545

4646
if let Node::Expr(parent) = cx.tcx.parent_hir_node(e.hir_id)
47-
&& parent.precedence() > ExprPrecedence::Cast
47+
&& cx.precedence(parent) > ExprPrecedence::Cast
4848
{
4949
sugg = format!("({sugg})");
5050
}

0 commit comments

Comments
 (0)