Minor code simplification

This commit is contained in:
Yuri Astrakhan 2024-09-01 20:58:14 -04:00
parent a81f1c8277
commit cd28176025
2 changed files with 17 additions and 23 deletions

View File

@ -26,7 +26,7 @@ pub(super) fn check<'cx>(cx: &LateContext<'cx>, name: Symbol, items: &[NestedMet
cx, cx,
ALLOW_ATTRIBUTES_WITHOUT_REASON, ALLOW_ATTRIBUTES_WITHOUT_REASON,
attr.span, attr.span,
format!("`{}` attribute without specifying a reason", name.as_str()), format!("`{name}` attribute without specifying a reason"),
|diag| { |diag| {
diag.help("try adding a reason at the end with `, reason = \"..\"`"); diag.help("try adding a reason at the end with `, reason = \"..\"`");
}, },

View File

@ -180,8 +180,10 @@ impl<'a> Sugg<'a> {
) -> Self { ) -> Self {
use rustc_ast::ast::RangeLimits; use rustc_ast::ast::RangeLimits;
let mut snippet = |span: Span| snippet_with_context(cx, span, ctxt, default, app).0;
match expr.kind { match expr.kind {
_ if expr.span.ctxt() != ctxt => Sugg::NonParen(snippet_with_context(cx, expr.span, ctxt, default, app).0), _ if expr.span.ctxt() != ctxt => Sugg::NonParen(snippet(expr.span)),
ast::ExprKind::AddrOf(..) ast::ExprKind::AddrOf(..)
| ast::ExprKind::Closure { .. } | ast::ExprKind::Closure { .. }
| ast::ExprKind::If(..) | ast::ExprKind::If(..)
@ -224,46 +226,38 @@ impl<'a> Sugg<'a> {
| ast::ExprKind::While(..) | ast::ExprKind::While(..)
| ast::ExprKind::Await(..) | ast::ExprKind::Await(..)
| ast::ExprKind::Err(_) | ast::ExprKind::Err(_)
| ast::ExprKind::Dummy => Sugg::NonParen(snippet_with_context(cx, expr.span, ctxt, default, app).0), | ast::ExprKind::Dummy => Sugg::NonParen(snippet(expr.span)),
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| { lhs.as_ref().map_or("".into(), |lhs| snippet(lhs.span)),
snippet_with_context(cx, lhs.span, ctxt, default, app).0 rhs.as_ref().map_or("".into(), |rhs| snippet(rhs.span)),
}),
rhs.as_ref().map_or("".into(), |rhs| {
snippet_with_context(cx, rhs.span, ctxt, default, app).0
}),
), ),
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| { lhs.as_ref().map_or("".into(), |lhs| snippet(lhs.span)),
snippet_with_context(cx, lhs.span, ctxt, default, app).0 rhs.as_ref().map_or("".into(), |rhs| snippet(rhs.span)),
}),
rhs.as_ref().map_or("".into(), |rhs| {
snippet_with_context(cx, rhs.span, ctxt, default, app).0
}),
), ),
ast::ExprKind::Assign(ref lhs, ref rhs, _) => Sugg::BinOp( ast::ExprKind::Assign(ref lhs, ref rhs, _) => Sugg::BinOp(
AssocOp::Assign, AssocOp::Assign,
snippet_with_context(cx, lhs.span, ctxt, default, app).0, snippet(lhs.span),
snippet_with_context(cx, rhs.span, ctxt, default, app).0, snippet(rhs.span),
), ),
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_with_context(cx, lhs.span, ctxt, default, app).0, snippet(lhs.span),
snippet_with_context(cx, rhs.span, ctxt, default, app).0, snippet(rhs.span),
), ),
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_with_context(cx, lhs.span, ctxt, default, app).0, snippet(lhs.span),
snippet_with_context(cx, rhs.span, ctxt, default, app).0, snippet(rhs.span),
), ),
ast::ExprKind::Cast(ref lhs, ref ty) | ast::ExprKind::Cast(ref lhs, ref ty) |
//FIXME(chenyukang), remove this after type ascription is removed from AST //FIXME(chenyukang), remove this after type ascription is removed from AST
ast::ExprKind::Type(ref lhs, ref ty) => Sugg::BinOp( ast::ExprKind::Type(ref lhs, ref ty) => Sugg::BinOp(
AssocOp::As, AssocOp::As,
snippet_with_context(cx, lhs.span, ctxt, default, app).0, snippet(lhs.span),
snippet_with_context(cx, ty.span, ctxt, default, app).0, snippet(ty.span),
), ),
} }
} }