Skip to content

Commit 71a7aef

Browse files
committed
Sugg: do not parenthesize a double unary operator
For example, adding `*` in front of `*expression` is best shown as `**expression` rather than `*(*expression)`.
1 parent f8a3929 commit 71a7aef

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

clippy_utils/src/sugg.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,17 @@ impl<T: Display> Display for ParenHelper<T> {
494494
/// operators have the same
495495
/// precedence.
496496
pub fn make_unop(op: &str, expr: Sugg<'_>) -> Sugg<'static> {
497-
Sugg::MaybeParen(format!("{op}{}", expr.maybe_paren()).into())
497+
// If the `expr` starts with `op` already, do not add wrap it in
498+
// parentheses.
499+
let expr = if let Sugg::MaybeParen(ref sugg) = expr
500+
&& !has_enclosing_paren(sugg)
501+
&& sugg.starts_with(op)
502+
{
503+
expr
504+
} else {
505+
expr.maybe_paren()
506+
};
507+
Sugg::MaybeParen(format!("{op}{expr}").into())
498508
}
499509

500510
/// Builds the string for `<lhs> <op> <rhs>` adding parenthesis when necessary.

tests/ui/nonminimal_bool.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ error: inequality checks against true can be replaced by a negation
179179
--> tests/ui/nonminimal_bool.rs:186:8
180180
|
181181
LL | if !b != true {}
182-
| ^^^^^^^^^^ help: try simplifying it as shown: `!(!b)`
182+
| ^^^^^^^^^^ help: try simplifying it as shown: `!!b`
183183

184184
error: this boolean expression can be simplified
185185
--> tests/ui/nonminimal_bool.rs:189:8
@@ -209,7 +209,7 @@ error: inequality checks against true can be replaced by a negation
209209
--> tests/ui/nonminimal_bool.rs:193:8
210210
|
211211
LL | if true != !b {}
212-
| ^^^^^^^^^^ help: try simplifying it as shown: `!(!b)`
212+
| ^^^^^^^^^^ help: try simplifying it as shown: `!!b`
213213

214214
error: this boolean expression can be simplified
215215
--> tests/ui/nonminimal_bool.rs:196:8

0 commit comments

Comments
 (0)