Skip to content

Commit a47fe3b

Browse files
committed
Dogfood fixes
1 parent 7e5f357 commit a47fe3b

File tree

1 file changed

+52
-42
lines changed

1 file changed

+52
-42
lines changed

clippy_macros/src/sugg.rs

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ op_precedence! {
111111
Suffix => Call,
112112
}
113113
impl ExprPrec {
114-
fn merge_with(&self, other: Self) -> Self {
115-
match (*self, other) {
114+
fn merge_with(self, other: Self) -> Self {
115+
match (self, other) {
116116
(Self::Suffix | Self::Field, Self::Suffix | Self::Field) => other,
117-
_ => core::cmp::min(*self, other),
117+
_ => core::cmp::min(self, other),
118118
}
119119
}
120120
}
@@ -178,7 +178,11 @@ macro_rules! bin_op {
178178
impl SuggBuilder {
179179
fn parse_bin_op(&mut self, input: ParseStream<'_>, var: Option<ExprVar>) -> Option<BinOp> {
180180
use ExprPos::*;
181-
$(if self.consume_op_token_space_prefixed::<Token![$($op_tt)*]>(input, var, concat_idents!($prec, Lhs)) {
181+
$(if self.consume_op_token_space_prefixed::<Token![$($op_tt)*]>(
182+
input,
183+
var,
184+
concat_idents!($prec, Lhs)
185+
) {
182186
self.next_string.push(' ');
183187
Some(BinOp {
184188
prec: ExprPrec::$prec,
@@ -335,7 +339,9 @@ impl SuggBuilder {
335339
}
336340

337341
fn require_token<T: Token>(&mut self, input: ParseStream<'_>, msg: &str) -> Result<()> {
338-
self.consume_token::<T>(input).then(|| ()).ok_or_else(|| input.error(msg))
342+
self.consume_token::<T>(input)
343+
.then(|| ())
344+
.ok_or_else(|| input.error(msg))
339345
}
340346

341347
fn consume_token<T: Token>(&mut self, input: ParseStream<'_>) -> bool {
@@ -430,6 +436,7 @@ impl SuggBuilder {
430436
self.require(input, Self::parse_ty, "expected a type").map(Some)
431437
}
432438

439+
#[allow(clippy::blocks_in_if_conditions)]
433440
fn parse_ty_body(&mut self, input: ParseStream) -> Result<Option<()>> {
434441
if let Some(ends_with_ident) = self.parse_path_root(input)? {
435442
if ends_with_ident && self.consume_token::<Token![<]>(input) {
@@ -440,19 +447,17 @@ impl SuggBuilder {
440447
input,
441448
|_| (),
442449
|self_, input| {
443-
self_.require(&input, Self::parse_ty, "expected a type")?;
444-
if self_.consume_token::<Token![;]>(&input) {
450+
self_.require(input, Self::parse_ty, "expected a type")?;
451+
if self_.consume_token::<Token![;]>(input) {
445452
self_.next_string.push(' ');
446-
self_.require(&input, Self::parse_expr, "expected an expression")?;
453+
self_.require(input, Self::parse_expr, "expected an expression")?;
447454
}
448455
Ok(())
449456
},
450-
)? {
451-
// Nothing to do
452-
} else if self.consume_group::<token::Paren>(
457+
)? || self.consume_group::<token::Paren>(
453458
input,
454459
|_| (),
455-
|self_, input| self_.parse_list(&input, Self::parse_ty),
460+
|self_, input| self_.parse_list(input, Self::parse_ty),
456461
)? {
457462
// Nothing to do
458463
} else if let Some(var) = parse_var(input) {
@@ -549,15 +554,14 @@ impl SuggBuilder {
549554
}
550555

551556
fn parse_expr_body(&mut self, input: ParseStream<'_>, pos: ExprPos, prec: ExprPrec) -> Result<Option<ExprPrec>> {
552-
if self.consume_token::<Literal>(input) {
553-
// Nothing to do
554-
} else if self.consume_group::<token::Paren>(
555-
input,
556-
|_| (),
557-
|self_, input| self_.parse_list(&input, Self::parse_expr).map(|_| ()),
558-
)? {
559-
// Nothing to do
560-
} else if self.parse_path_root(input)?.is_some() {
557+
if self.consume_token::<Literal>(input)
558+
|| self.consume_group::<token::Paren>(
559+
input,
560+
|_| (),
561+
|self_, input| self_.parse_list(input, Self::parse_expr).map(|_| ()),
562+
)?
563+
|| self.parse_path_root(input)?.is_some()
564+
{
561565
// Nothing to do
562566
} else if let Some(var) = parse_var(input) {
563567
return if matches!(var.kind, VarKind::Expr | VarKind::Default) {
@@ -585,19 +589,22 @@ impl SuggBuilder {
585589
self.next_string.push(' ');
586590
self.require(input, Self::parse_ty, "expected a type")?;
587591
prec.merge_with(ExprPrec::Cast)
588-
} else if self.consume_op_token::<Token![?]>(input, var, ExprPos::Suffix) {
589-
prec.merge_with(ExprPrec::Suffix)
590-
} else if self.consume_group::<token::Bracket>(
591-
input,
592-
|self_| self_.push_expr_var(var, ExprPos::Suffix),
593-
|self_, input| self_.require(&input, Self::parse_expr, "expected an expression").map(|_| ()),
594-
)? {
595-
prec.merge_with(ExprPrec::Suffix)
596-
} else if self.consume_group::<token::Paren>(
597-
input,
598-
|self_| self_.push_expr_var(var, ExprPos::Callee),
599-
|self_, input| self_.parse_list(&input, Self::parse_expr).map(|_| ()),
600-
)? {
592+
} else if self.consume_op_token::<Token![?]>(input, var, ExprPos::Suffix)
593+
|| self.consume_group::<token::Bracket>(
594+
input,
595+
|self_| self_.push_expr_var(var, ExprPos::Suffix),
596+
|self_, input| {
597+
self_
598+
.require(input, Self::parse_expr, "expected an expression")
599+
.map(|_| ())
600+
},
601+
)?
602+
|| self.consume_group::<token::Paren>(
603+
input,
604+
|self_| self_.push_expr_var(var, ExprPos::Callee),
605+
|self_, input| self_.parse_list(input, Self::parse_expr).map(|_| ()),
606+
)?
607+
{
601608
prec.merge_with(ExprPrec::Suffix)
602609
} else if self.consume_op_token::<Token![.]>(input, var, ExprPos::Suffix) {
603610
if self.consume_token::<Ident>(input) {
@@ -700,22 +707,24 @@ impl SuggBuilder {
700707
let s = &self.next_string;
701708
body.extend(iter::once(quote!(sugg.push_str(#s);)));
702709
}
703-
if prec != ExprPrec::Suffix {
710+
if prec == ExprPrec::Suffix {
711+
body.extend(iter::once(quote!(sugg)));
712+
} else {
704713
body.extend(iter::once(quote!(
705714
if clippy_utils::_internal::needs_parens(#prec, clippy_utils::_internal::expr_position(cx, e)) {
706715
format!("({})", sugg)
707716
} else {
708717
sugg
709718
}
710719
)));
711-
} else {
712-
body.extend(iter::once(quote!(sugg)));
713720
}
714-
Ok(quote!(|cx: &rustc_lint::LateContext<'_>, e: &rustc_hir::Expr<'_>, app: &mut rustc_errors::Applicability| {
715-
let ctxt = e.span.ctxt();
716-
let mut sugg = String::new();
717-
#body
718-
}))
721+
Ok(
722+
quote!(|cx: &rustc_lint::LateContext<'_>, e: &rustc_hir::Expr<'_>, app: &mut rustc_errors::Applicability| {
723+
let ctxt = e.span.ctxt();
724+
let mut sugg = String::new();
725+
#body
726+
}),
727+
)
719728
}
720729
}
721730

@@ -742,6 +751,7 @@ fn split_args(input: ParseStream) -> Result<Vec<TokenStream>> {
742751
}
743752
}
744753

754+
#[allow(clippy::module_name_repetitions)]
745755
pub struct ExprSugg(pub TokenStream);
746756
impl Parse for ExprSugg {
747757
fn parse(input: ParseStream<'_>) -> Result<Self> {

0 commit comments

Comments
 (0)