Skip to content

Commit c1cd64b

Browse files
committed
Auto merge of #8117 - hotate29:issue7320, r=camsteffen
update: ```Sugg::not()``` replacing the comparison operator. #7320 fixes #7320 changelog: ```needless_bool```: Changed to make a smart suggestion.
2 parents 16ef044 + a172439 commit c1cd64b

11 files changed

+307
-128
lines changed

Diff for: clippy_lints/src/floating_point_arithmetic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ fn detect_hypot(cx: &LateContext<'_>, args: &[Expr<'_>]) -> Option<String> {
356356
if eq_expr_value(cx, lmul_lhs, lmul_rhs);
357357
if eq_expr_value(cx, rmul_lhs, rmul_rhs);
358358
then {
359-
return Some(format!("{}.hypot({})", Sugg::hir(cx, lmul_lhs, ".."), Sugg::hir(cx, rmul_lhs, "..")));
359+
return Some(format!("{}.hypot({})", Sugg::hir(cx, lmul_lhs, "..").maybe_par(), Sugg::hir(cx, rmul_lhs, "..")));
360360
}
361361
}
362362

@@ -379,7 +379,7 @@ fn detect_hypot(cx: &LateContext<'_>, args: &[Expr<'_>]) -> Option<String> {
379379
if let Some((rvalue, _)) = constant(cx, cx.typeck_results(), rargs_1);
380380
if Int(2) == lvalue && Int(2) == rvalue;
381381
then {
382-
return Some(format!("{}.hypot({})", Sugg::hir(cx, largs_0, ".."), Sugg::hir(cx, rargs_0, "..")));
382+
return Some(format!("{}.hypot({})", Sugg::hir(cx, largs_0, "..").maybe_par(), Sugg::hir(cx, rargs_0, "..")));
383383
}
384384
}
385385
}

Diff for: clippy_lints/src/loops/manual_memcpy.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_hir::{BinOpKind, Block, Expr, ExprKind, HirId, Pat, PatKind, StmtKind}
1212
use rustc_lint::LateContext;
1313
use rustc_middle::ty::{self, Ty};
1414
use rustc_span::symbol::sym;
15+
use std::fmt::Display;
1516
use std::iter::Iterator;
1617

1718
/// Checks for for loops that sequentially copy items from one slice-like
@@ -108,7 +109,7 @@ fn build_manual_memcpy_suggestion<'tcx>(
108109
src: &IndexExpr<'_>,
109110
) -> String {
110111
fn print_offset(offset: MinifyingSugg<'static>) -> MinifyingSugg<'static> {
111-
if offset.as_str() == "0" {
112+
if offset.to_string() == "0" {
112113
sugg::EMPTY.into()
113114
} else {
114115
offset
@@ -123,7 +124,7 @@ fn build_manual_memcpy_suggestion<'tcx>(
123124
if let Some(arg) = len_args.get(0);
124125
if path_to_local(arg) == path_to_local(base);
125126
then {
126-
if sugg.as_str() == end_str {
127+
if sugg.to_string() == end_str {
127128
sugg::EMPTY.into()
128129
} else {
129130
sugg
@@ -147,7 +148,7 @@ fn build_manual_memcpy_suggestion<'tcx>(
147148
print_offset(apply_offset(&start_str, &idx_expr.idx_offset)).into_sugg(),
148149
print_limit(
149150
end,
150-
end_str.as_str(),
151+
end_str.to_string().as_str(),
151152
idx_expr.base,
152153
apply_offset(&end_str, &idx_expr.idx_offset),
153154
)
@@ -159,7 +160,7 @@ fn build_manual_memcpy_suggestion<'tcx>(
159160
print_offset(apply_offset(&counter_start, &idx_expr.idx_offset)).into_sugg(),
160161
print_limit(
161162
end,
162-
end_str.as_str(),
163+
end_str.to_string().as_str(),
163164
idx_expr.base,
164165
apply_offset(&end_str, &idx_expr.idx_offset) + &counter_start - &start_str,
165166
)
@@ -202,12 +203,13 @@ fn build_manual_memcpy_suggestion<'tcx>(
202203
#[derive(Clone)]
203204
struct MinifyingSugg<'a>(Sugg<'a>);
204205

205-
impl<'a> MinifyingSugg<'a> {
206-
fn as_str(&self) -> &str {
207-
let (Sugg::NonParen(s) | Sugg::MaybeParen(s) | Sugg::BinOp(_, s)) = &self.0;
208-
s.as_ref()
206+
impl Display for MinifyingSugg<'a> {
207+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
208+
self.0.fmt(f)
209209
}
210+
}
210211

212+
impl<'a> MinifyingSugg<'a> {
211213
fn into_sugg(self) -> Sugg<'a> {
212214
self.0
213215
}
@@ -222,7 +224,7 @@ impl<'a> From<Sugg<'a>> for MinifyingSugg<'a> {
222224
impl std::ops::Add for &MinifyingSugg<'static> {
223225
type Output = MinifyingSugg<'static>;
224226
fn add(self, rhs: &MinifyingSugg<'static>) -> MinifyingSugg<'static> {
225-
match (self.as_str(), rhs.as_str()) {
227+
match (self.to_string().as_str(), rhs.to_string().as_str()) {
226228
("0", _) => rhs.clone(),
227229
(_, "0") => self.clone(),
228230
(_, _) => (&self.0 + &rhs.0).into(),
@@ -233,7 +235,7 @@ impl std::ops::Add for &MinifyingSugg<'static> {
233235
impl std::ops::Sub for &MinifyingSugg<'static> {
234236
type Output = MinifyingSugg<'static>;
235237
fn sub(self, rhs: &MinifyingSugg<'static>) -> MinifyingSugg<'static> {
236-
match (self.as_str(), rhs.as_str()) {
238+
match (self.to_string().as_str(), rhs.to_string().as_str()) {
237239
(_, "0") => self.clone(),
238240
("0", _) => (-rhs.0.clone()).into(),
239241
(x, y) if x == y => sugg::ZERO.into(),
@@ -245,7 +247,7 @@ impl std::ops::Sub for &MinifyingSugg<'static> {
245247
impl std::ops::Add<&MinifyingSugg<'static>> for MinifyingSugg<'static> {
246248
type Output = MinifyingSugg<'static>;
247249
fn add(self, rhs: &MinifyingSugg<'static>) -> MinifyingSugg<'static> {
248-
match (self.as_str(), rhs.as_str()) {
250+
match (self.to_string().as_str(), rhs.to_string().as_str()) {
249251
("0", _) => rhs.clone(),
250252
(_, "0") => self,
251253
(_, _) => (self.0 + &rhs.0).into(),
@@ -256,7 +258,7 @@ impl std::ops::Add<&MinifyingSugg<'static>> for MinifyingSugg<'static> {
256258
impl std::ops::Sub<&MinifyingSugg<'static>> for MinifyingSugg<'static> {
257259
type Output = MinifyingSugg<'static>;
258260
fn sub(self, rhs: &MinifyingSugg<'static>) -> MinifyingSugg<'static> {
259-
match (self.as_str(), rhs.as_str()) {
261+
match (self.to_string().as_str(), rhs.to_string().as_str()) {
260262
(_, "0") => self,
261263
("0", _) => (-rhs.0.clone()).into(),
262264
(x, y) if x == y => sugg::ZERO.into(),

Diff for: clippy_lints/src/needless_bool.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,14 @@ impl<'tcx> LateLintPass<'tcx> for BoolComparison {
187187
BinOpKind::Eq => {
188188
let true_case = Some((|h| h, "equality checks against true are unnecessary"));
189189
let false_case = Some((
190-
|h: Sugg<'_>| !h,
190+
|h: Sugg<'tcx>| !h,
191191
"equality checks against false can be replaced by a negation",
192192
));
193193
check_comparison(cx, e, true_case, false_case, true_case, false_case, ignore_no_literal);
194194
},
195195
BinOpKind::Ne => {
196196
let true_case = Some((
197-
|h: Sugg<'_>| !h,
197+
|h: Sugg<'tcx>| !h,
198198
"inequality checks against true can be replaced by a negation",
199199
));
200200
let false_case = Some((|h| h, "inequality checks against false are unnecessary"));
@@ -206,27 +206,27 @@ impl<'tcx> LateLintPass<'tcx> for BoolComparison {
206206
ignore_case,
207207
Some((|h| h, "greater than checks against false are unnecessary")),
208208
Some((
209-
|h: Sugg<'_>| !h,
209+
|h: Sugg<'tcx>| !h,
210210
"less than comparison against true can be replaced by a negation",
211211
)),
212212
ignore_case,
213213
Some((
214-
|l: Sugg<'_>, r: Sugg<'_>| (!l).bit_and(&r),
214+
|l: Sugg<'tcx>, r: Sugg<'tcx>| (!l).bit_and(&r),
215215
"order comparisons between booleans can be simplified",
216216
)),
217217
),
218218
BinOpKind::Gt => check_comparison(
219219
cx,
220220
e,
221221
Some((
222-
|h: Sugg<'_>| !h,
222+
|h: Sugg<'tcx>| !h,
223223
"less than comparison against true can be replaced by a negation",
224224
)),
225225
ignore_case,
226226
ignore_case,
227227
Some((|h| h, "greater than checks against false are unnecessary")),
228228
Some((
229-
|l: Sugg<'_>, r: Sugg<'_>| l.bit_and(&(!r)),
229+
|l: Sugg<'tcx>, r: Sugg<'tcx>| l.bit_and(&(!r)),
230230
"order comparisons between booleans can be simplified",
231231
)),
232232
),

Diff for: clippy_lints/src/ranges.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,8 @@ fn check_exclusive_range_plus_one(cx: &LateContext<'_>, expr: &Expr<'_>) {
378378
span,
379379
"an inclusive range would be more readable",
380380
|diag| {
381-
let start = start.map_or(String::new(), |x| Sugg::hir(cx, x, "x").to_string());
382-
let end = Sugg::hir(cx, y, "y");
381+
let start = start.map_or(String::new(), |x| Sugg::hir(cx, x, "x").maybe_par().to_string());
382+
let end = Sugg::hir(cx, y, "y").maybe_par();
383383
if let Some(is_wrapped) = &snippet_opt(cx, span) {
384384
if is_wrapped.starts_with('(') && is_wrapped.ends_with(')') {
385385
diag.span_suggestion(
@@ -415,8 +415,8 @@ fn check_inclusive_range_minus_one(cx: &LateContext<'_>, expr: &Expr<'_>) {
415415
expr.span,
416416
"an exclusive range would be more readable",
417417
|diag| {
418-
let start = start.map_or(String::new(), |x| Sugg::hir(cx, x, "x").to_string());
419-
let end = Sugg::hir(cx, y, "y");
418+
let start = start.map_or(String::new(), |x| Sugg::hir(cx, x, "x").maybe_par().to_string());
419+
let end = Sugg::hir(cx, y, "y").maybe_par();
420420
diag.span_suggestion(
421421
expr.span,
422422
"use",

0 commit comments

Comments
 (0)