Skip to content

Commit 69f6009

Browse files
committed
Auto merge of rust-lang#9410 - dswij:issue-9375, r=xFrednet
Use macro callsite when creating `Sugg` helper Closes rust-lang#9375 changelog: Improvement: [`collapsible_if`]: Suggestions now work with macros, by taking the call site into account.
2 parents c8c2a23 + f0d642e commit 69f6009

File tree

4 files changed

+42
-22
lines changed

4 files changed

+42
-22
lines changed

clippy_utils/src/sugg.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use std::fmt::{Display, Write as _};
2222
use std::ops::{Add, Neg, Not, Sub};
2323

2424
/// A helper type to build suggestion correctly handling parentheses.
25-
#[derive(Clone, PartialEq)]
25+
#[derive(Clone, Debug, PartialEq)]
2626
pub enum Sugg<'a> {
2727
/// An expression that never needs parentheses such as `1337` or `[0; 42]`.
2828
NonParen(Cow<'a, str>),
@@ -177,11 +177,11 @@ impl<'a> Sugg<'a> {
177177
pub fn ast(cx: &EarlyContext<'_>, expr: &ast::Expr, default: &'a str) -> Self {
178178
use rustc_ast::ast::RangeLimits;
179179

180-
let get_whole_snippet = || {
181-
if expr.span.from_expansion() {
182-
snippet_with_macro_callsite(cx, expr.span, default)
180+
let snippet_without_expansion = |cx, span: Span, default| {
181+
if span.from_expansion() {
182+
snippet_with_macro_callsite(cx, span, default)
183183
} else {
184-
snippet(cx, expr.span, default)
184+
snippet(cx, span, default)
185185
}
186186
};
187187

@@ -192,7 +192,7 @@ impl<'a> Sugg<'a> {
192192
| ast::ExprKind::If(..)
193193
| ast::ExprKind::Let(..)
194194
| ast::ExprKind::Unary(..)
195-
| ast::ExprKind::Match(..) => Sugg::MaybeParen(get_whole_snippet()),
195+
| ast::ExprKind::Match(..) => Sugg::MaybeParen(snippet_without_expansion(cx, expr.span, default)),
196196
ast::ExprKind::Async(..)
197197
| ast::ExprKind::Block(..)
198198
| ast::ExprKind::Break(..)
@@ -221,41 +221,45 @@ impl<'a> Sugg<'a> {
221221
| ast::ExprKind::Array(..)
222222
| ast::ExprKind::While(..)
223223
| ast::ExprKind::Await(..)
224-
| ast::ExprKind::Err => Sugg::NonParen(get_whole_snippet()),
224+
| ast::ExprKind::Err => Sugg::NonParen(snippet_without_expansion(cx, expr.span, default)),
225225
ast::ExprKind::Range(ref lhs, ref rhs, RangeLimits::HalfOpen) => Sugg::BinOp(
226226
AssocOp::DotDot,
227-
lhs.as_ref().map_or("".into(), |lhs| snippet(cx, lhs.span, default)),
228-
rhs.as_ref().map_or("".into(), |rhs| snippet(cx, rhs.span, default)),
227+
lhs.as_ref()
228+
.map_or("".into(), |lhs| snippet_without_expansion(cx, lhs.span, default)),
229+
rhs.as_ref()
230+
.map_or("".into(), |rhs| snippet_without_expansion(cx, rhs.span, default)),
229231
),
230232
ast::ExprKind::Range(ref lhs, ref rhs, RangeLimits::Closed) => Sugg::BinOp(
231233
AssocOp::DotDotEq,
232-
lhs.as_ref().map_or("".into(), |lhs| snippet(cx, lhs.span, default)),
233-
rhs.as_ref().map_or("".into(), |rhs| snippet(cx, rhs.span, default)),
234+
lhs.as_ref()
235+
.map_or("".into(), |lhs| snippet_without_expansion(cx, lhs.span, default)),
236+
rhs.as_ref()
237+
.map_or("".into(), |rhs| snippet_without_expansion(cx, rhs.span, default)),
234238
),
235239
ast::ExprKind::Assign(ref lhs, ref rhs, _) => Sugg::BinOp(
236240
AssocOp::Assign,
237-
snippet(cx, lhs.span, default),
238-
snippet(cx, rhs.span, default),
241+
snippet_without_expansion(cx, lhs.span, default),
242+
snippet_without_expansion(cx, rhs.span, default),
239243
),
240244
ast::ExprKind::AssignOp(op, ref lhs, ref rhs) => Sugg::BinOp(
241245
astbinop2assignop(op),
242-
snippet(cx, lhs.span, default),
243-
snippet(cx, rhs.span, default),
246+
snippet_without_expansion(cx, lhs.span, default),
247+
snippet_without_expansion(cx, rhs.span, default),
244248
),
245249
ast::ExprKind::Binary(op, ref lhs, ref rhs) => Sugg::BinOp(
246250
AssocOp::from_ast_binop(op.node),
247-
snippet(cx, lhs.span, default),
248-
snippet(cx, rhs.span, default),
251+
snippet_without_expansion(cx, lhs.span, default),
252+
snippet_without_expansion(cx, rhs.span, default),
249253
),
250254
ast::ExprKind::Cast(ref lhs, ref ty) => Sugg::BinOp(
251255
AssocOp::As,
252-
snippet(cx, lhs.span, default),
253-
snippet(cx, ty.span, default),
256+
snippet_without_expansion(cx, lhs.span, default),
257+
snippet_without_expansion(cx, ty.span, default),
254258
),
255259
ast::ExprKind::Type(ref lhs, ref ty) => Sugg::BinOp(
256260
AssocOp::Colon,
257-
snippet(cx, lhs.span, default),
258-
snippet(cx, ty.span, default),
261+
snippet_without_expansion(cx, lhs.span, default),
262+
snippet_without_expansion(cx, ty.span, default),
259263
),
260264
}
261265
}

tests/ui/collapsible_if.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ fn main() {
139139
// Fix #5962
140140
if matches!(true, true) && matches!(true, true) {}
141141

142+
// Issue #9375
143+
if matches!(true, true) && truth() && matches!(true, true) {}
144+
142145
if true {
143146
#[cfg(not(teehee))]
144147
if true {

tests/ui/collapsible_if.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ fn main() {
155155
if matches!(true, true) {}
156156
}
157157

158+
// Issue #9375
159+
if matches!(true, true) && truth() {
160+
if matches!(true, true) {}
161+
}
162+
158163
if true {
159164
#[cfg(not(teehee))]
160165
if true {

tests/ui/collapsible_if.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,13 @@ LL | | if matches!(true, true) {}
126126
LL | | }
127127
| |_____^ help: collapse nested if block: `if matches!(true, true) && matches!(true, true) {}`
128128

129-
error: aborting due to 8 previous errors
129+
error: this `if` statement can be collapsed
130+
--> $DIR/collapsible_if.rs:159:5
131+
|
132+
LL | / if matches!(true, true) && truth() {
133+
LL | | if matches!(true, true) {}
134+
LL | | }
135+
| |_____^ help: collapse nested if block: `if matches!(true, true) && truth() && matches!(true, true) {}`
136+
137+
error: aborting due to 9 previous errors
130138

0 commit comments

Comments
 (0)