Use macro source when creating Sugg helper

This commit is contained in:
dswij 2022-09-01 00:47:56 +08:00
parent f51aade56f
commit f0d642ea38
4 changed files with 42 additions and 22 deletions

View file

@ -22,7 +22,7 @@ use std::fmt::{Display, Write as _};
use std::ops::{Add, Neg, Not, Sub}; use std::ops::{Add, Neg, Not, Sub};
/// A helper type to build suggestion correctly handling parentheses. /// A helper type to build suggestion correctly handling parentheses.
#[derive(Clone, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub enum Sugg<'a> { pub enum Sugg<'a> {
/// An expression that never needs parentheses such as `1337` or `[0; 42]`. /// An expression that never needs parentheses such as `1337` or `[0; 42]`.
NonParen(Cow<'a, str>), NonParen(Cow<'a, str>),
@ -177,11 +177,11 @@ impl<'a> Sugg<'a> {
pub fn ast(cx: &EarlyContext<'_>, expr: &ast::Expr, default: &'a str) -> Self { pub fn ast(cx: &EarlyContext<'_>, expr: &ast::Expr, default: &'a str) -> Self {
use rustc_ast::ast::RangeLimits; use rustc_ast::ast::RangeLimits;
let get_whole_snippet = || { let snippet_without_expansion = |cx, span: Span, default| {
if expr.span.from_expansion() { if span.from_expansion() {
snippet_with_macro_callsite(cx, expr.span, default) snippet_with_macro_callsite(cx, span, default)
} else { } else {
snippet(cx, expr.span, default) snippet(cx, span, default)
} }
}; };
@ -192,7 +192,7 @@ impl<'a> Sugg<'a> {
| ast::ExprKind::If(..) | ast::ExprKind::If(..)
| ast::ExprKind::Let(..) | ast::ExprKind::Let(..)
| ast::ExprKind::Unary(..) | ast::ExprKind::Unary(..)
| ast::ExprKind::Match(..) => Sugg::MaybeParen(get_whole_snippet()), | ast::ExprKind::Match(..) => Sugg::MaybeParen(snippet_without_expansion(cx, expr.span, default)),
ast::ExprKind::Async(..) ast::ExprKind::Async(..)
| ast::ExprKind::Block(..) | ast::ExprKind::Block(..)
| ast::ExprKind::Break(..) | ast::ExprKind::Break(..)
@ -221,41 +221,45 @@ impl<'a> Sugg<'a> {
| ast::ExprKind::Array(..) | ast::ExprKind::Array(..)
| ast::ExprKind::While(..) | ast::ExprKind::While(..)
| ast::ExprKind::Await(..) | ast::ExprKind::Await(..)
| ast::ExprKind::Err => Sugg::NonParen(get_whole_snippet()), | ast::ExprKind::Err => Sugg::NonParen(snippet_without_expansion(cx, expr.span, default)),
ast::ExprKind::Range(ref lhs, ref rhs, RangeLimits::HalfOpen) => Sugg::BinOp( ast::ExprKind::Range(ref lhs, ref rhs, RangeLimits::HalfOpen) => Sugg::BinOp(
AssocOp::DotDot, AssocOp::DotDot,
lhs.as_ref().map_or("".into(), |lhs| snippet(cx, lhs.span, default)), lhs.as_ref()
rhs.as_ref().map_or("".into(), |rhs| snippet(cx, rhs.span, default)), .map_or("".into(), |lhs| snippet_without_expansion(cx, lhs.span, default)),
rhs.as_ref()
.map_or("".into(), |rhs| snippet_without_expansion(cx, rhs.span, default)),
), ),
ast::ExprKind::Range(ref lhs, ref rhs, RangeLimits::Closed) => Sugg::BinOp( ast::ExprKind::Range(ref lhs, ref rhs, RangeLimits::Closed) => Sugg::BinOp(
AssocOp::DotDotEq, AssocOp::DotDotEq,
lhs.as_ref().map_or("".into(), |lhs| snippet(cx, lhs.span, default)), lhs.as_ref()
rhs.as_ref().map_or("".into(), |rhs| snippet(cx, rhs.span, default)), .map_or("".into(), |lhs| snippet_without_expansion(cx, lhs.span, default)),
rhs.as_ref()
.map_or("".into(), |rhs| snippet_without_expansion(cx, rhs.span, default)),
), ),
ast::ExprKind::Assign(ref lhs, ref rhs, _) => Sugg::BinOp( ast::ExprKind::Assign(ref lhs, ref rhs, _) => Sugg::BinOp(
AssocOp::Assign, AssocOp::Assign,
snippet(cx, lhs.span, default), snippet_without_expansion(cx, lhs.span, default),
snippet(cx, rhs.span, default), snippet_without_expansion(cx, rhs.span, default),
), ),
ast::ExprKind::AssignOp(op, ref lhs, ref rhs) => Sugg::BinOp( ast::ExprKind::AssignOp(op, ref lhs, ref rhs) => Sugg::BinOp(
astbinop2assignop(op), astbinop2assignop(op),
snippet(cx, lhs.span, default), snippet_without_expansion(cx, lhs.span, default),
snippet(cx, rhs.span, default), snippet_without_expansion(cx, rhs.span, default),
), ),
ast::ExprKind::Binary(op, ref lhs, ref rhs) => Sugg::BinOp( ast::ExprKind::Binary(op, ref lhs, ref rhs) => Sugg::BinOp(
AssocOp::from_ast_binop(op.node), AssocOp::from_ast_binop(op.node),
snippet(cx, lhs.span, default), snippet_without_expansion(cx, lhs.span, default),
snippet(cx, rhs.span, default), snippet_without_expansion(cx, rhs.span, default),
), ),
ast::ExprKind::Cast(ref lhs, ref ty) => Sugg::BinOp( ast::ExprKind::Cast(ref lhs, ref ty) => Sugg::BinOp(
AssocOp::As, AssocOp::As,
snippet(cx, lhs.span, default), snippet_without_expansion(cx, lhs.span, default),
snippet(cx, ty.span, default), snippet_without_expansion(cx, ty.span, default),
), ),
ast::ExprKind::Type(ref lhs, ref ty) => Sugg::BinOp( ast::ExprKind::Type(ref lhs, ref ty) => Sugg::BinOp(
AssocOp::Colon, AssocOp::Colon,
snippet(cx, lhs.span, default), snippet_without_expansion(cx, lhs.span, default),
snippet(cx, ty.span, default), snippet_without_expansion(cx, ty.span, default),
), ),
} }
} }

View file

@ -139,6 +139,9 @@ fn main() {
// Fix #5962 // Fix #5962
if matches!(true, true) && matches!(true, true) {} if matches!(true, true) && matches!(true, true) {}
// Issue #9375
if matches!(true, true) && truth() && matches!(true, true) {}
if true { if true {
#[cfg(not(teehee))] #[cfg(not(teehee))]
if true { if true {

View file

@ -155,6 +155,11 @@ fn main() {
if matches!(true, true) {} if matches!(true, true) {}
} }
// Issue #9375
if matches!(true, true) && truth() {
if matches!(true, true) {}
}
if true { if true {
#[cfg(not(teehee))] #[cfg(not(teehee))]
if true { if true {

View file

@ -126,5 +126,13 @@ LL | | if matches!(true, true) {}
LL | | } LL | | }
| |_____^ help: collapse nested if block: `if matches!(true, true) && matches!(true, true) {}` | |_____^ help: collapse nested if block: `if matches!(true, true) && matches!(true, true) {}`
error: aborting due to 8 previous errors error: this `if` statement can be collapsed
--> $DIR/collapsible_if.rs:159:5
|
LL | / if matches!(true, true) && truth() {
LL | | if matches!(true, true) {}
LL | | }
| |_____^ help: collapse nested if block: `if matches!(true, true) && truth() && matches!(true, true) {}`
error: aborting due to 9 previous errors