Skip to content

Commit 478db11

Browse files
committed
Auto merge of #137710 - matthiaskrgr:rollup-3vmxxu9, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - #136542 ([`compiletest`-related cleanups 4/7] Make the distinction between root build directory vs test suite specific build directory in compiletest less confusing) - #136579 (Fix UB in ThinVec::flat_map_in_place) - #136688 (require trait impls to have matching const stabilities as the traits) - #136846 (Make `AssocOp` more like `ExprKind`) - #137304 (add `IntoBounds::intersect` and `RangeBounds::is_empty`) - #137455 (Reuse machinery from `tail_expr_drop_order` for `if_let_rescope`) - #137480 (Return unexpected termination error instead of panicing in `Thread::join`) - #137694 (Spruce up `AttributeKind` docs) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 22eac98 + bb42df5 commit 478db11

File tree

3 files changed

+43
-123
lines changed

3 files changed

+43
-123
lines changed

clippy_lints/src/operators/float_equality_without_abs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub(crate) fn check<'tcx>(
5050
// format the suggestion
5151
let suggestion = format!(
5252
"{}.abs()",
53-
sugg::make_assoc(AssocOp::Subtract, &sug_l, &sug_r).maybe_par()
53+
sugg::make_assoc(AssocOp::Binary(BinOpKind::Sub), &sug_l, &sug_r).maybe_par()
5454
);
5555
// spans the lint
5656
span_lint_and_then(

clippy_utils/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
// (Currently there is no way to opt into sysroot crates without `extern crate`.)
3232
extern crate rustc_abi;
3333
extern crate rustc_ast;
34-
extern crate rustc_ast_pretty;
3534
extern crate rustc_attr_parsing;
3635
extern crate rustc_const_eval;
3736
extern crate rustc_data_structures;

clippy_utils/src/sugg.rs

Lines changed: 42 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use crate::source::{snippet, snippet_opt, snippet_with_applicability, snippet_wi
55
use crate::ty::expr_sig;
66
use crate::{get_parent_expr_for_hir, higher};
77
use rustc_ast::util::parser::AssocOp;
8-
use rustc_ast::{ast, token};
9-
use rustc_ast_pretty::pprust::token_kind_to_string;
8+
use rustc_ast::ast;
109
use rustc_errors::Applicability;
1110
use rustc_hir as hir;
1211
use rustc_hir::{Closure, ExprKind, HirId, MutTy, TyKind};
@@ -114,10 +113,7 @@ impl<'a> Sugg<'a> {
114113
/// function variants of `Sugg`, since these use different snippet functions.
115114
fn hir_from_snippet(expr: &hir::Expr<'_>, mut get_snippet: impl FnMut(Span) -> Cow<'a, str>) -> Self {
116115
if let Some(range) = higher::Range::hir(expr) {
117-
let op = match range.limits {
118-
ast::RangeLimits::HalfOpen => AssocOp::DotDot,
119-
ast::RangeLimits::Closed => AssocOp::DotDotEq,
120-
};
116+
let op = AssocOp::Range(range.limits);
121117
let start = range.start.map_or("".into(), |expr| get_snippet(expr.span));
122118
let end = range.end.map_or("".into(), |expr| get_snippet(expr.span));
123119

@@ -158,16 +154,16 @@ impl<'a> Sugg<'a> {
158154
Sugg::BinOp(AssocOp::Assign, get_snippet(lhs.span), get_snippet(rhs.span))
159155
},
160156
ExprKind::AssignOp(op, lhs, rhs) => {
161-
Sugg::BinOp(hirbinop2assignop(op), get_snippet(lhs.span), get_snippet(rhs.span))
157+
Sugg::BinOp(AssocOp::AssignOp(op.node), get_snippet(lhs.span), get_snippet(rhs.span))
162158
},
163159
ExprKind::Binary(op, lhs, rhs) => Sugg::BinOp(
164-
AssocOp::from_ast_binop(op.node),
160+
AssocOp::Binary(op.node),
165161
get_snippet(lhs.span),
166162
get_snippet(rhs.span),
167163
),
168164
ExprKind::Cast(lhs, ty) |
169165
//FIXME(chenyukang), remove this after type ascription is removed from AST
170-
ExprKind::Type(lhs, ty) => Sugg::BinOp(AssocOp::As, get_snippet(lhs.span), get_snippet(ty.span)),
166+
ExprKind::Type(lhs, ty) => Sugg::BinOp(AssocOp::Cast, get_snippet(lhs.span), get_snippet(ty.span)),
171167
}
172168
}
173169

@@ -179,8 +175,6 @@ impl<'a> Sugg<'a> {
179175
ctxt: SyntaxContext,
180176
app: &mut Applicability,
181177
) -> Self {
182-
use rustc_ast::ast::RangeLimits;
183-
184178
let mut snippet = |span: Span| snippet_with_context(cx, span, ctxt, default, app).0;
185179

186180
match expr.kind {
@@ -229,13 +223,8 @@ impl<'a> Sugg<'a> {
229223
| ast::ExprKind::Err(_)
230224
| ast::ExprKind::Dummy
231225
| ast::ExprKind::UnsafeBinderCast(..) => Sugg::NonParen(snippet(expr.span)),
232-
ast::ExprKind::Range(ref lhs, ref rhs, RangeLimits::HalfOpen) => Sugg::BinOp(
233-
AssocOp::DotDot,
234-
lhs.as_ref().map_or("".into(), |lhs| snippet(lhs.span)),
235-
rhs.as_ref().map_or("".into(), |rhs| snippet(rhs.span)),
236-
),
237-
ast::ExprKind::Range(ref lhs, ref rhs, RangeLimits::Closed) => Sugg::BinOp(
238-
AssocOp::DotDotEq,
226+
ast::ExprKind::Range(ref lhs, ref rhs, limits) => Sugg::BinOp(
227+
AssocOp::Range(limits),
239228
lhs.as_ref().map_or("".into(), |lhs| snippet(lhs.span)),
240229
rhs.as_ref().map_or("".into(), |rhs| snippet(rhs.span)),
241230
),
@@ -245,19 +234,19 @@ impl<'a> Sugg<'a> {
245234
snippet(rhs.span),
246235
),
247236
ast::ExprKind::AssignOp(op, ref lhs, ref rhs) => Sugg::BinOp(
248-
astbinop2assignop(op),
237+
AssocOp::AssignOp(op.node),
249238
snippet(lhs.span),
250239
snippet(rhs.span),
251240
),
252241
ast::ExprKind::Binary(op, ref lhs, ref rhs) => Sugg::BinOp(
253-
AssocOp::from_ast_binop(op.node),
242+
AssocOp::Binary(op.node),
254243
snippet(lhs.span),
255244
snippet(rhs.span),
256245
),
257246
ast::ExprKind::Cast(ref lhs, ref ty) |
258247
//FIXME(chenyukang), remove this after type ascription is removed from AST
259248
ast::ExprKind::Type(ref lhs, ref ty) => Sugg::BinOp(
260-
AssocOp::As,
249+
AssocOp::Cast,
261250
snippet(lhs.span),
262251
snippet(ty.span),
263252
),
@@ -276,7 +265,7 @@ impl<'a> Sugg<'a> {
276265

277266
/// Convenience method to create the `<lhs> as <rhs>` suggestion.
278267
pub fn as_ty<R: Display>(self, rhs: R) -> Sugg<'static> {
279-
make_assoc(AssocOp::As, &self, &Sugg::NonParen(rhs.to_string().into()))
268+
make_assoc(AssocOp::Cast, &self, &Sugg::NonParen(rhs.to_string().into()))
280269
}
281270

282271
/// Convenience method to create the `&<expr>` suggestion.
@@ -327,11 +316,8 @@ impl<'a> Sugg<'a> {
327316

328317
/// Convenience method to create the `<lhs>..<rhs>` or `<lhs>...<rhs>`
329318
/// suggestion.
330-
pub fn range(self, end: &Self, limit: ast::RangeLimits) -> Sugg<'static> {
331-
match limit {
332-
ast::RangeLimits::HalfOpen => make_assoc(AssocOp::DotDot, &self, end),
333-
ast::RangeLimits::Closed => make_assoc(AssocOp::DotDotEq, &self, end),
334-
}
319+
pub fn range(self, end: &Self, limits: ast::RangeLimits) -> Sugg<'static> {
320+
make_assoc(AssocOp::Range(limits), &self, end)
335321
}
336322

337323
/// Adds parentheses to any expression that might need them. Suitable to the
@@ -367,33 +353,11 @@ impl<'a> Sugg<'a> {
367353
/// Generates a string from the operator and both sides.
368354
fn binop_to_string(op: AssocOp, lhs: &str, rhs: &str) -> String {
369355
match op {
370-
AssocOp::Add
371-
| AssocOp::Subtract
372-
| AssocOp::Multiply
373-
| AssocOp::Divide
374-
| AssocOp::Modulus
375-
| AssocOp::LAnd
376-
| AssocOp::LOr
377-
| AssocOp::BitXor
378-
| AssocOp::BitAnd
379-
| AssocOp::BitOr
380-
| AssocOp::ShiftLeft
381-
| AssocOp::ShiftRight
382-
| AssocOp::Equal
383-
| AssocOp::Less
384-
| AssocOp::LessEqual
385-
| AssocOp::NotEqual
386-
| AssocOp::Greater
387-
| AssocOp::GreaterEqual => {
388-
format!("{lhs} {} {rhs}", op.to_ast_binop().expect("Those are AST ops").as_str())
389-
},
356+
AssocOp::Binary(op) => format!("{lhs} {} {rhs}", op.as_str()),
390357
AssocOp::Assign => format!("{lhs} = {rhs}"),
391-
AssocOp::AssignOp(op) => {
392-
format!("{lhs} {}= {rhs}", token_kind_to_string(&token::BinOp(op)))
393-
},
394-
AssocOp::As => format!("{lhs} as {rhs}"),
395-
AssocOp::DotDot => format!("{lhs}..{rhs}"),
396-
AssocOp::DotDotEq => format!("{lhs}..={rhs}"),
358+
AssocOp::AssignOp(op) => format!("{lhs} {}= {rhs}", op.as_str()),
359+
AssocOp::Cast => format!("{lhs} as {rhs}"),
360+
AssocOp::Range(limits) => format!("{lhs}{}{rhs}", limits.as_str()),
397361
}
398362
}
399363

@@ -468,7 +432,7 @@ impl Neg for Sugg<'_> {
468432
type Output = Sugg<'static>;
469433
fn neg(self) -> Sugg<'static> {
470434
match &self {
471-
Self::BinOp(AssocOp::As, ..) => Sugg::MaybeParen(format!("-({self})").into()),
435+
Self::BinOp(AssocOp::Cast, ..) => Sugg::MaybeParen(format!("-({self})").into()),
472436
_ => make_unop("-", self),
473437
}
474438
}
@@ -477,16 +441,17 @@ impl Neg for Sugg<'_> {
477441
impl<'a> Not for Sugg<'a> {
478442
type Output = Sugg<'a>;
479443
fn not(self) -> Sugg<'a> {
480-
use AssocOp::{Equal, Greater, GreaterEqual, Less, LessEqual, NotEqual};
444+
use AssocOp::Binary;
445+
use ast::BinOpKind::{Eq, Gt, Ge, Lt, Le, Ne};
481446

482447
if let Sugg::BinOp(op, lhs, rhs) = self {
483448
let to_op = match op {
484-
Equal => NotEqual,
485-
NotEqual => Equal,
486-
Less => GreaterEqual,
487-
GreaterEqual => Less,
488-
Greater => LessEqual,
489-
LessEqual => Greater,
449+
Binary(Eq) => Binary(Ne),
450+
Binary(Ne) => Binary(Eq),
451+
Binary(Lt) => Binary(Ge),
452+
Binary(Ge) => Binary(Lt),
453+
Binary(Gt) => Binary(Le),
454+
Binary(Le) => Binary(Gt),
490455
_ => return make_unop("!", Sugg::BinOp(op, lhs, rhs)),
491456
};
492457
Sugg::BinOp(to_op, lhs, rhs)
@@ -538,15 +503,21 @@ pub fn make_unop(op: &str, expr: Sugg<'_>) -> Sugg<'static> {
538503
pub fn make_assoc(op: AssocOp, lhs: &Sugg<'_>, rhs: &Sugg<'_>) -> Sugg<'static> {
539504
/// Returns `true` if the operator is a shift operator `<<` or `>>`.
540505
fn is_shift(op: AssocOp) -> bool {
541-
matches!(op, AssocOp::ShiftLeft | AssocOp::ShiftRight)
506+
matches!(op, AssocOp::Binary(ast::BinOpKind::Shl | ast::BinOpKind::Shr))
542507
}
543508

544509
/// Returns `true` if the operator is an arithmetic operator
545510
/// (i.e., `+`, `-`, `*`, `/`, `%`).
546511
fn is_arith(op: AssocOp) -> bool {
547512
matches!(
548513
op,
549-
AssocOp::Add | AssocOp::Subtract | AssocOp::Multiply | AssocOp::Divide | AssocOp::Modulus
514+
AssocOp::Binary(
515+
ast::BinOpKind::Add
516+
| ast::BinOpKind::Sub
517+
| ast::BinOpKind::Mul
518+
| ast::BinOpKind::Div
519+
| ast::BinOpKind::Rem
520+
)
550521
)
551522
}
552523

@@ -578,9 +549,9 @@ pub fn make_assoc(op: AssocOp, lhs: &Sugg<'_>, rhs: &Sugg<'_>) -> Sugg<'static>
578549
Sugg::BinOp(op, lhs.into(), rhs.into())
579550
}
580551

581-
/// Convenience wrapper around `make_assoc` and `AssocOp::from_ast_binop`.
552+
/// Convenience wrapper around `make_assoc` and `AssocOp::Binary`.
582553
pub fn make_binop(op: ast::BinOpKind, lhs: &Sugg<'_>, rhs: &Sugg<'_>) -> Sugg<'static> {
583-
make_assoc(AssocOp::from_ast_binop(op), lhs, rhs)
554+
make_assoc(AssocOp::Binary(op), lhs, rhs)
584555
}
585556

586557
#[derive(PartialEq, Eq, Clone, Copy)]
@@ -605,69 +576,19 @@ enum Associativity {
605576
/// associative.
606577
#[must_use]
607578
fn associativity(op: AssocOp) -> Associativity {
608-
use rustc_ast::util::parser::AssocOp::{
609-
Add, As, Assign, AssignOp, BitAnd, BitOr, BitXor, Divide, DotDot, DotDotEq, Equal, Greater, GreaterEqual, LAnd,
610-
LOr, Less, LessEqual, Modulus, Multiply, NotEqual, ShiftLeft, ShiftRight, Subtract,
579+
use rustc_ast::util::parser::AssocOp::{Assign, AssignOp, Binary, Cast, Range};
580+
use ast::BinOpKind::{
581+
Add, BitAnd, BitOr, BitXor, Div, Eq, Gt, Ge, And, Or, Lt, Le, Rem, Mul, Ne, Shl, Shr, Sub,
611582
};
612583

613584
match op {
614585
Assign | AssignOp(_) => Associativity::Right,
615-
Add | BitAnd | BitOr | BitXor | LAnd | LOr | Multiply | As => Associativity::Both,
616-
Divide | Equal | Greater | GreaterEqual | Less | LessEqual | Modulus | NotEqual | ShiftLeft | ShiftRight
617-
| Subtract => Associativity::Left,
618-
DotDot | DotDotEq => Associativity::None,
586+
Binary(Add | BitAnd | BitOr | BitXor | And | Or | Mul) | Cast => Associativity::Both,
587+
Binary(Div | Eq | Gt | Ge | Lt | Le | Rem | Ne | Shl | Shr | Sub) => Associativity::Left,
588+
Range(_) => Associativity::None,
619589
}
620590
}
621591

622-
/// Converts a `hir::BinOp` to the corresponding assigning binary operator.
623-
fn hirbinop2assignop(op: hir::BinOp) -> AssocOp {
624-
use rustc_ast::token::BinOpToken::{And, Caret, Minus, Or, Percent, Plus, Shl, Shr, Slash, Star};
625-
626-
AssocOp::AssignOp(match op.node {
627-
hir::BinOpKind::Add => Plus,
628-
hir::BinOpKind::BitAnd => And,
629-
hir::BinOpKind::BitOr => Or,
630-
hir::BinOpKind::BitXor => Caret,
631-
hir::BinOpKind::Div => Slash,
632-
hir::BinOpKind::Mul => Star,
633-
hir::BinOpKind::Rem => Percent,
634-
hir::BinOpKind::Shl => Shl,
635-
hir::BinOpKind::Shr => Shr,
636-
hir::BinOpKind::Sub => Minus,
637-
638-
hir::BinOpKind::And
639-
| hir::BinOpKind::Eq
640-
| hir::BinOpKind::Ge
641-
| hir::BinOpKind::Gt
642-
| hir::BinOpKind::Le
643-
| hir::BinOpKind::Lt
644-
| hir::BinOpKind::Ne
645-
| hir::BinOpKind::Or => panic!("This operator does not exist"),
646-
})
647-
}
648-
649-
/// Converts an `ast::BinOp` to the corresponding assigning binary operator.
650-
fn astbinop2assignop(op: ast::BinOp) -> AssocOp {
651-
use rustc_ast::ast::BinOpKind::{
652-
Add, And, BitAnd, BitOr, BitXor, Div, Eq, Ge, Gt, Le, Lt, Mul, Ne, Or, Rem, Shl, Shr, Sub,
653-
};
654-
use rustc_ast::token::BinOpToken;
655-
656-
AssocOp::AssignOp(match op.node {
657-
Add => BinOpToken::Plus,
658-
BitAnd => BinOpToken::And,
659-
BitOr => BinOpToken::Or,
660-
BitXor => BinOpToken::Caret,
661-
Div => BinOpToken::Slash,
662-
Mul => BinOpToken::Star,
663-
Rem => BinOpToken::Percent,
664-
Shl => BinOpToken::Shl,
665-
Shr => BinOpToken::Shr,
666-
Sub => BinOpToken::Minus,
667-
And | Eq | Ge | Gt | Le | Lt | Ne | Or => panic!("This operator does not exist"),
668-
})
669-
}
670-
671592
/// Returns the indentation before `span` if there are nothing but `[ \t]`
672593
/// before it on its line.
673594
fn indentation<T: LintContext>(cx: &T, span: Span) -> Option<String> {

0 commit comments

Comments
 (0)