Skip to content

Commit 4ffdce0

Browse files
committed
refactor: use clippy_utils::Sugg instead of direct string ops
1 parent 9925600 commit 4ffdce0

File tree

3 files changed

+21
-26
lines changed

3 files changed

+21
-26
lines changed

clippy_lints/src/bool_to_int_with_if.rs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use rustc_ast::{ExprPrecedence, LitKind};
1+
use rustc_ast::LitKind;
22
use rustc_hir::{Block, ExprKind};
33
use rustc_lint::{LateContext, LateLintPass};
44
use rustc_session::{declare_lint_pass, declare_tool_lint};
55

6-
use clippy_utils::{diagnostics::span_lint_and_then, is_else_clause, source::snippet_block_with_applicability};
6+
use clippy_utils::{diagnostics::span_lint_and_then, is_else_clause, sugg::Sugg};
77
use rustc_errors::Applicability;
88

99
declare_clippy_lint! {
@@ -69,28 +69,27 @@ fn check_if_else<'tcx>(ctx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx
6969
return;
7070
};
7171
let mut applicability = Applicability::MachineApplicable;
72-
let snippet = snippet_block_with_applicability(ctx, check.span, "..", None, &mut applicability);
73-
74-
let invert = if inverted { "!" } else { "" };
75-
let need_parens = should_have_parentheses(check);
76-
77-
let snippet_with_braces = {
78-
let (left_paren, right_paren) = if need_parens {("(", ")")} else {("", "")};
79-
format!("{invert}{left_paren}{snippet}{right_paren}")
72+
let snippet = {
73+
let mut sugg = Sugg::hir_with_applicability(ctx, check, "..", &mut applicability);
74+
if inverted {
75+
sugg = !sugg;
76+
}
77+
sugg
8078
};
8179

8280
let ty = ctx.typeck_results().expr_ty(then_lit); // then and else must be of same type
8381

8482
let suggestion = {
8583
let wrap_in_curly = is_else_clause(ctx.tcx, expr);
86-
let (left_curly, right_curly) = if wrap_in_curly {("{", "}")} else {("", "")};
87-
let (left_paren, right_paren) = if inverted && need_parens {("(", ")")} else {("", "")};
88-
format!(
89-
"{left_curly}{ty}::from({invert}{left_paren}{snippet}{right_paren}){right_curly}"
90-
)
84+
let mut s = Sugg::NonParen(format!("{ty}::from({snippet})").into());
85+
if wrap_in_curly {
86+
s = s.blockify();
87+
}
88+
s
9189
}; // when used in else clause if statement should be wrapped in curly braces
9290

93-
let (inverted_left_paren, inverted_right_paren) = if inverted {("(", ")")} else {("", "")};
91+
let into_snippet = snippet.clone().maybe_par();
92+
let as_snippet = snippet.as_ty(ty);
9493

9594
span_lint_and_then(ctx,
9695
BOOL_TO_INT_WITH_IF,
@@ -103,7 +102,7 @@ fn check_if_else<'tcx>(ctx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx
103102
suggestion,
104103
applicability,
105104
);
106-
diag.note(format!("`{snippet_with_braces} as {ty}` or `{inverted_left_paren}{snippet_with_braces}{inverted_right_paren}.into()` can also be valid options"));
105+
diag.note(format!("`{as_snippet}` or `{into_snippet}.into()` can also be valid options"));
107106
});
108107
};
109108
}
@@ -135,7 +134,3 @@ fn check_int_literal_equals_val<'tcx>(expr: &'tcx rustc_hir::Expr<'tcx>, expecte
135134
false
136135
}
137136
}
138-
139-
fn should_have_parentheses<'tcx>(check: &'tcx rustc_hir::Expr<'tcx>) -> bool {
140-
check.precedence().order() < ExprPrecedence::Cast.order()
141-
}

tests/ui/bool_to_int_with_if.fixed

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ fn main() {
2222
// if else if
2323
if a {
2424
123
25-
} else {i32::from(b)};
25+
} else { i32::from(b) };
2626

2727
// if else if inverted
2828
if a {
2929
123
30-
} else {i32::from(!b)};
30+
} else { i32::from(!b) };
3131

3232
// Shouldn't lint
3333

tests/ui/bool_to_int_with_if.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ LL | | 0
3333
LL | | };
3434
| |_____^ help: replace with from: `i32::from(!a)`
3535
|
36-
= note: `!a as i32` or `!a.into()` can also be valid options
36+
= note: `!a as i32` or `(!a).into()` can also be valid options
3737

3838
error: boolean to int conversion using if
3939
--> $DIR/bool_to_int_with_if.rs:30:5
@@ -80,7 +80,7 @@ LL | | 1
8080
LL | | } else {
8181
LL | | 0
8282
LL | | };
83-
| |_____^ help: replace with from: `{i32::from(b)}`
83+
| |_____^ help: replace with from: `{ i32::from(b) }`
8484
|
8585
= note: `b as i32` or `b.into()` can also be valid options
8686

@@ -93,7 +93,7 @@ LL | | 0
9393
LL | | } else {
9494
LL | | 1
9595
LL | | };
96-
| |_____^ help: replace with from: `{i32::from(!b)}`
96+
| |_____^ help: replace with from: `{ i32::from(!b) }`
9797
|
9898
= note: `!b as i32` or `(!b).into()` can also be valid options
9999

0 commit comments

Comments
 (0)