Skip to content

Commit bc01b2e

Browse files
committed
Auto merge of rust-lang#101562 - nnethercote:shrink-ast-Expr-harder, r=petrochenkov
Shrink `ast::Expr` harder r? `@ghost`
2 parents f7535e7 + 333b92c commit bc01b2e

8 files changed

+50
-28
lines changed

clippy_lints/src/double_parens.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ impl EarlyLintPass for DoubleParens {
6161
}
6262
}
6363
},
64-
ExprKind::MethodCall(_, _, ref params, _) => {
65-
if let [ref param] = params[..] {
66-
if let ExprKind::Paren(_) = param.kind {
67-
span_lint(cx, DOUBLE_PARENS, param.span, msg);
64+
ExprKind::MethodCall(ref call) => {
65+
if let [ref arg] = call.args[..] {
66+
if let ExprKind::Paren(_) = arg.kind {
67+
span_lint(cx, DOUBLE_PARENS, arg.span, msg);
6868
}
6969
}
7070
},

clippy_lints/src/option_env_unwrap.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::is_direct_expn_of;
33
use if_chain::if_chain;
4-
use rustc_ast::ast::{Expr, ExprKind};
4+
use rustc_ast::ast::{Expr, ExprKind, MethodCall};
55
use rustc_lint::{EarlyContext, EarlyLintPass};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
77
use rustc_span::sym;
@@ -37,8 +37,8 @@ declare_lint_pass!(OptionEnvUnwrap => [OPTION_ENV_UNWRAP]);
3737
impl EarlyLintPass for OptionEnvUnwrap {
3838
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
3939
if_chain! {
40-
if let ExprKind::MethodCall(path_segment, receiver, _, _) = &expr.kind;
41-
if matches!(path_segment.ident.name, sym::expect | sym::unwrap);
40+
if let ExprKind::MethodCall(box MethodCall { seg, receiver, .. }) = &expr.kind;
41+
if matches!(seg.ident.name, sym::expect | sym::unwrap);
4242
if let ExprKind::Call(caller, _) = &receiver.kind;
4343
if is_direct_expn_of(caller.span, "option_env").is_some();
4444
then {

clippy_lints/src/precedence.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_applicability;
33
use if_chain::if_chain;
4-
use rustc_ast::ast::{BinOpKind, Expr, ExprKind, UnOp};
4+
use rustc_ast::ast::{BinOpKind, Expr, ExprKind, MethodCall, UnOp};
55
use rustc_ast::token;
66
use rustc_errors::Applicability;
77
use rustc_lint::{EarlyContext, EarlyLintPass};
@@ -110,11 +110,11 @@ impl EarlyLintPass for Precedence {
110110
let mut arg = operand;
111111

112112
let mut all_odd = true;
113-
while let ExprKind::MethodCall(path_segment, receiver, _, _) = &arg.kind {
114-
let path_segment_str = path_segment.ident.name.as_str();
113+
while let ExprKind::MethodCall(box MethodCall { seg, receiver, .. }) = &arg.kind {
114+
let seg_str = seg.ident.name.as_str();
115115
all_odd &= ALLOWED_ODD_FUNCTIONS
116116
.iter()
117-
.any(|odd_function| **odd_function == *path_segment_str);
117+
.any(|odd_function| **odd_function == *seg_str);
118118
arg = receiver;
119119
}
120120

clippy_lints/src/redundant_closure_call.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,24 @@ impl EarlyLintPass for RedundantClosureCall {
6969
if_chain! {
7070
if let ast::ExprKind::Call(ref paren, _) = expr.kind;
7171
if let ast::ExprKind::Paren(ref closure) = paren.kind;
72-
if let ast::ExprKind::Closure(_, _, ref r#async, _, ref decl, ref block, _) = closure.kind;
72+
if let ast::ExprKind::Closure(box ast::Closure { ref asyncness, ref fn_decl, ref body, .. }) = closure.kind;
7373
then {
7474
let mut visitor = ReturnVisitor::new();
75-
visitor.visit_expr(block);
75+
visitor.visit_expr(body);
7676
if !visitor.found_return {
7777
span_lint_and_then(
7878
cx,
7979
REDUNDANT_CLOSURE_CALL,
8080
expr.span,
8181
"try not to call a closure in the expression where it is declared",
8282
|diag| {
83-
if decl.inputs.is_empty() {
83+
if fn_decl.inputs.is_empty() {
8484
let app = Applicability::MachineApplicable;
85-
let mut hint = Sugg::ast(cx, block, "..");
85+
let mut hint = Sugg::ast(cx, body, "..");
8686

87-
if r#async.is_async() {
87+
if asyncness.is_async() {
8888
// `async x` is a syntax error, so it becomes `async { x }`
89-
if !matches!(block.kind, ast::ExprKind::Block(_, _)) {
89+
if !matches!(body.kind, ast::ExprKind::Block(_, _)) {
9090
hint = hint.blockify();
9191
}
9292

clippy_lints/src/suspicious_operation_groupings.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ fn ident_difference_expr_with_base_location(
580580
| (Await(_), Await(_))
581581
| (Async(_, _, _), Async(_, _, _))
582582
| (Block(_, _), Block(_, _))
583-
| (Closure(_, _, _, _, _, _, _), Closure(_, _, _, _, _, _, _))
583+
| (Closure(_), Closure(_))
584584
| (Match(_, _), Match(_, _))
585585
| (Loop(_, _), Loop(_, _))
586586
| (ForLoop(_, _, _, _), ForLoop(_, _, _, _))
@@ -593,7 +593,7 @@ fn ident_difference_expr_with_base_location(
593593
| (Unary(_, _), Unary(_, _))
594594
| (Binary(_, _, _), Binary(_, _, _))
595595
| (Tup(_), Tup(_))
596-
| (MethodCall(_, _, _, _), MethodCall(_, _, _, _))
596+
| (MethodCall(_), MethodCall(_))
597597
| (Call(_, _), Call(_, _))
598598
| (ConstBlock(_), ConstBlock(_))
599599
| (Array(_), Array(_))

clippy_lints/src/unnested_or_patterns.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ fn transform_with_focus_on_idx(alternatives: &mut Vec<P<Pat>>, focus_idx: usize)
292292
/// So when we fixate on some `ident_k: pat_k`, we try to find `ident_k` in the other pattern
293293
/// and check that all `fp_i` where `i ∈ ((0...n) \ k)` between two patterns are equal.
294294
fn extend_with_struct_pat(
295-
qself1: &Option<ast::QSelf>,
295+
qself1: &Option<P<ast::QSelf>>,
296296
path1: &ast::Path,
297297
fps1: &mut [ast::PatField],
298298
rest1: bool,

clippy_lints/src/unused_rounding.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use rustc_ast::ast::{Expr, ExprKind};
2+
use rustc_ast::ast::{Expr, ExprKind, MethodCall};
33
use rustc_errors::Applicability;
44
use rustc_lint::{EarlyContext, EarlyLintPass};
55
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -30,8 +30,8 @@ declare_clippy_lint! {
3030
declare_lint_pass!(UnusedRounding => [UNUSED_ROUNDING]);
3131

3232
fn is_useless_rounding(expr: &Expr) -> Option<(&str, String)> {
33-
if let ExprKind::MethodCall(name_ident, receiver, _, _) = &expr.kind
34-
&& let method_name = name_ident.ident.name.as_str()
33+
if let ExprKind::MethodCall(box MethodCall { seg, receiver, .. }) = &expr.kind
34+
&& let method_name = seg.ident.name.as_str()
3535
&& (method_name == "ceil" || method_name == "round" || method_name == "floor")
3636
&& let ExprKind::Lit(token_lit) = &receiver.kind
3737
&& token_lit.is_semantic_float() {

clippy_utils/src/ast_utils.rs

+27-5
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ pub fn eq_field_pat(l: &PatField, r: &PatField) -> bool {
7575
&& over(&l.attrs, &r.attrs, eq_attr)
7676
}
7777

78-
pub fn eq_qself(l: &QSelf, r: &QSelf) -> bool {
78+
pub fn eq_qself(l: &P<QSelf>, r: &P<QSelf>) -> bool {
7979
l.position == r.position && eq_ty(&l.ty, &r.ty)
8080
}
8181

82-
pub fn eq_maybe_qself(l: &Option<QSelf>, r: &Option<QSelf>) -> bool {
82+
pub fn eq_maybe_qself(l: &Option<P<QSelf>>, r: &Option<P<QSelf>>) -> bool {
8383
match (l, r) {
8484
(Some(l), Some(r)) => eq_qself(l, r),
8585
(None, None) => true,
@@ -147,8 +147,11 @@ pub fn eq_expr(l: &Expr, r: &Expr) -> bool {
147147
(Array(l), Array(r)) | (Tup(l), Tup(r)) => over(l, r, |l, r| eq_expr(l, r)),
148148
(Repeat(le, ls), Repeat(re, rs)) => eq_expr(le, re) && eq_expr(&ls.value, &rs.value),
149149
(Call(lc, la), Call(rc, ra)) => eq_expr(lc, rc) && over(la, ra, |l, r| eq_expr(l, r)),
150-
(MethodCall(lc, ls, la, _), MethodCall(rc, rs, ra, _)) => {
151-
eq_path_seg(lc, rc) && eq_expr(ls, rs) && over(la, ra, |l, r| eq_expr(l, r))
150+
(
151+
MethodCall(box ast::MethodCall { seg: ls, receiver: lr, args: la, .. }),
152+
MethodCall(box ast::MethodCall { seg: rs, receiver: rr, args: ra, .. })
153+
) => {
154+
eq_path_seg(ls, rs) && eq_expr(lr, rr) && over(la, ra, |l, r| eq_expr(l, r))
152155
},
153156
(Binary(lo, ll, lr), Binary(ro, rl, rr)) => lo.node == ro.node && eq_expr(ll, rl) && eq_expr(lr, rr),
154157
(Unary(lo, l), Unary(ro, r)) => mem::discriminant(lo) == mem::discriminant(ro) && eq_expr(l, r),
@@ -170,7 +173,26 @@ pub fn eq_expr(l: &Expr, r: &Expr) -> bool {
170173
(AssignOp(lo, lp, lv), AssignOp(ro, rp, rv)) => lo.node == ro.node && eq_expr(lp, rp) && eq_expr(lv, rv),
171174
(Field(lp, lf), Field(rp, rf)) => eq_id(*lf, *rf) && eq_expr(lp, rp),
172175
(Match(ls, la), Match(rs, ra)) => eq_expr(ls, rs) && over(la, ra, eq_arm),
173-
(Closure(lb, lc, la, lm, lf, le, _), Closure(rb, rc, ra, rm, rf, re, _)) => {
176+
(
177+
Closure(box ast::Closure {
178+
binder: lb,
179+
capture_clause: lc,
180+
asyncness: la,
181+
movability: lm,
182+
fn_decl: lf,
183+
body: le,
184+
..
185+
}),
186+
Closure(box ast::Closure {
187+
binder: rb,
188+
capture_clause: rc,
189+
asyncness: ra,
190+
movability: rm,
191+
fn_decl: rf,
192+
body: re,
193+
..
194+
})
195+
) => {
174196
eq_closure_binder(lb, rb)
175197
&& lc == rc
176198
&& la.is_async() == ra.is_async()

0 commit comments

Comments
 (0)