Skip to content

Commit 72eb60a

Browse files
committed
Auto merge of rust-lang#7013 - Y-Nak:fix-needless-paren, r=flip1995
clippy_utils: fix needless parenthesis output from sugg::Sugg::maybe_par changelog: clippy_utils: fix needless parenthesis output from `sugg::Sugg::maybe_par` fixes: rust-lang#6767
2 parents 92c4fc3 + 6325fe1 commit 72eb60a

File tree

5 files changed

+44
-6
lines changed

5 files changed

+44
-6
lines changed

clippy_utils/src/sugg.rs

+40-2
Original file line numberDiff line numberDiff line change
@@ -267,17 +267,44 @@ impl<'a> Sugg<'a> {
267267
Sugg::NonParen(..) => self,
268268
// `(x)` and `(x).y()` both don't need additional parens.
269269
Sugg::MaybeParen(sugg) => {
270-
if sugg.starts_with('(') && sugg.ends_with(')') {
270+
if has_enclosing_paren(&sugg) {
271271
Sugg::MaybeParen(sugg)
272272
} else {
273273
Sugg::NonParen(format!("({})", sugg).into())
274274
}
275275
},
276-
Sugg::BinOp(_, sugg) => Sugg::NonParen(format!("({})", sugg).into()),
276+
Sugg::BinOp(_, sugg) => {
277+
if has_enclosing_paren(&sugg) {
278+
Sugg::NonParen(sugg)
279+
} else {
280+
Sugg::NonParen(format!("({})", sugg).into())
281+
}
282+
},
277283
}
278284
}
279285
}
280286

287+
/// Return `true` if `sugg` is enclosed in parenthesis.
288+
fn has_enclosing_paren(sugg: impl AsRef<str>) -> bool {
289+
let mut chars = sugg.as_ref().chars();
290+
if let Some('(') = chars.next() {
291+
let mut depth = 1;
292+
while let Some(c) = chars.next() {
293+
if c == '(' {
294+
depth += 1;
295+
} else if c == ')' {
296+
depth -= 1;
297+
}
298+
if depth == 0 {
299+
break;
300+
}
301+
}
302+
chars.next().is_none()
303+
} else {
304+
false
305+
}
306+
}
307+
281308
// Copied from the rust standart library, and then edited
282309
macro_rules! forward_binop_impls_to_ref {
283310
(impl $imp:ident, $method:ident for $t:ty, type Output = $o:ty) => {
@@ -668,6 +695,8 @@ impl<T: LintContext> DiagnosticBuilderExt<T> for rustc_errors::DiagnosticBuilder
668695
#[cfg(test)]
669696
mod test {
670697
use super::Sugg;
698+
699+
use rustc_ast::util::parser::AssocOp;
671700
use std::borrow::Cow;
672701

673702
const SUGGESTION: Sugg<'static> = Sugg::NonParen(Cow::Borrowed("function_call()"));
@@ -681,4 +710,13 @@ mod test {
681710
fn blockify_transforms_sugg_into_a_block() {
682711
assert_eq!("{ function_call() }", SUGGESTION.blockify().to_string());
683712
}
713+
714+
#[test]
715+
fn binop_maybe_par() {
716+
let sugg = Sugg::BinOp(AssocOp::Add, "(1 + 1)".into());
717+
assert_eq!("(1 + 1)", sugg.maybe_par().to_string());
718+
719+
let sugg = Sugg::BinOp(AssocOp::Add, "(1 + 1) + (1 + 1)".into());
720+
assert_eq!("((1 + 1) + (1 + 1))", sugg.maybe_par().to_string());
721+
}
684722
}

tests/ui/floating_point_log.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn check_ln1p() {
2727
let _ = (x / 2.0).ln_1p();
2828
let _ = x.powi(3).ln_1p();
2929
let _ = (x.powi(3) / 2.0).ln_1p();
30-
let _ = ((std::f32::consts::E - 1.0)).ln_1p();
30+
let _ = (std::f32::consts::E - 1.0).ln_1p();
3131
let _ = x.ln_1p();
3232
let _ = x.powi(3).ln_1p();
3333
let _ = (x + 2.0).ln_1p();

tests/ui/floating_point_log.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ error: ln(1 + x) can be computed more accurately
9090
--> $DIR/floating_point_log.rs:30:13
9191
|
9292
LL | let _ = (1.0 + (std::f32::consts::E - 1.0)).ln();
93-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `((std::f32::consts::E - 1.0)).ln_1p()`
93+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(std::f32::consts::E - 1.0).ln_1p()`
9494

9595
error: ln(1 + x) can be computed more accurately
9696
--> $DIR/floating_point_log.rs:31:13

tests/ui/from_str_radix_10.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ error: this call to `from_str_radix` can be replaced with a call to `str::parse`
2828
--> $DIR/from_str_radix_10.rs:32:5
2929
|
3030
LL | u16::from_str_radix(&("10".to_owned() + "5"), 10)?;
31-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(("10".to_owned() + "5")).parse::<u16>()`
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `("10".to_owned() + "5").parse::<u16>()`
3232

3333
error: this call to `from_str_radix` can be replaced with a call to `str::parse`
3434
--> $DIR/from_str_radix_10.rs:33:5

tests/ui/manual_memcpy/with_loop_counters.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ LL | / for i in 3..(3 + src.len()) {
4343
LL | | dst[i] = src[count];
4444
LL | | count += 1;
4545
LL | | }
46-
| |_____^ help: try replacing the loop by: `dst[3..((3 + src.len()))].clone_from_slice(&src[..((3 + src.len()) - 3)]);`
46+
| |_____^ help: try replacing the loop by: `dst[3..(3 + src.len())].clone_from_slice(&src[..((3 + src.len()) - 3)]);`
4747

4848
error: it looks like you're manually copying between slices
4949
--> $DIR/with_loop_counters.rs:35:5

0 commit comments

Comments
 (0)