Skip to content

Commit 55f479b

Browse files
committed
use expr_sugg on various lints
1 parent e771b77 commit 55f479b

10 files changed

+42
-74
lines changed

clippy_lints/src/from_str_radix_10.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
use clippy_macros::expr_sugg;
12
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::sugg::Sugg;
33
use clippy_utils::ty::is_type_diagnostic_item;
44
use if_chain::if_chain;
55
use rustc_errors::Applicability;
@@ -76,20 +76,17 @@ impl LateLintPass<'tcx> for FromStrRadix10 {
7676
&arguments[0]
7777
};
7878

79-
let sugg = Sugg::hir_with_applicability(
80-
cx,
81-
expr,
82-
"<string>",
83-
&mut Applicability::MachineApplicable
84-
).maybe_par();
85-
79+
let mut app = Applicability::MachineApplicable;
8680
span_lint_and_sugg(
8781
cx,
8882
FROM_STR_RADIX_10,
8983
exp.span,
9084
"this call to `from_str_radix` can be replaced with a call to `str::parse`",
9185
"try",
92-
format!("{}.parse::<{}>()", sugg, prim_ty.name_str()),
86+
expr_sugg!(
87+
cx, &mut app, exp.span.ctxt(),
88+
$expr(expr).parse::<$ident(prim_ty.name_str())>()
89+
).into_string(),
9390
Applicability::MaybeIncorrect
9491
);
9592
}

clippy_lints/src/methods/bytes_nth.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
use clippy_macros::expr_sugg;
12
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::source::snippet_with_applicability;
33
use clippy_utils::ty::is_type_diagnostic_item;
44
use rustc_errors::Applicability;
55
use rustc_hir::Expr;
@@ -17,18 +17,14 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, recv: &'tcx E
1717
} else {
1818
return;
1919
};
20-
let mut applicability = Applicability::MachineApplicable;
20+
let mut app = Applicability::MachineApplicable;
2121
span_lint_and_sugg(
2222
cx,
2323
BYTES_NTH,
2424
expr.span,
25-
&format!("called `.byte().nth()` on a `{}`", caller_type),
25+
&format!("called `.bytes().nth()` on a `{}`", caller_type),
2626
"try",
27-
format!(
28-
"{}.as_bytes().get({})",
29-
snippet_with_applicability(cx, recv.span, "..", &mut applicability),
30-
snippet_with_applicability(cx, n_arg.span, "..", &mut applicability)
31-
),
32-
applicability,
27+
expr_sugg!(cx, &mut app, expr.span.ctxt(), $expr(recv).as_bytes().get($expr(n_arg))).into_string(),
28+
app,
3329
);
3430
}

clippy_lints/src/methods/iter_count.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::utils::derefs_to_slice;
2+
use clippy_macros::expr_sugg;
23
use clippy_utils::diagnostics::span_lint_and_sugg;
3-
use clippy_utils::source::snippet_with_applicability;
44
use clippy_utils::ty::is_type_diagnostic_item;
55
use rustc_errors::Applicability;
66
use rustc_hir::Expr;
@@ -32,17 +32,14 @@ pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, recv: &'tcx E
3232
} else {
3333
return;
3434
};
35-
let mut applicability = Applicability::MachineApplicable;
35+
let mut app = Applicability::MachineApplicable;
3636
span_lint_and_sugg(
3737
cx,
3838
ITER_COUNT,
3939
expr.span,
4040
&format!("called `.{}().count()` on a `{}`", iter_method, caller_type),
4141
"try",
42-
format!(
43-
"{}.len()",
44-
snippet_with_applicability(cx, recv.span, "..", &mut applicability),
45-
),
46-
applicability,
42+
expr_sugg!(cx, &mut app, expr.span.ctxt(), $expr(recv).len()).into_string(),
43+
app,
4744
);
4845
}

clippy_lints/src/methods/iter_nth_zero.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use clippy_macros::expr_sugg;
12
use clippy_utils::consts::{constant, Constant};
23
use clippy_utils::diagnostics::span_lint_and_sugg;
34
use clippy_utils::is_trait_method;
4-
use clippy_utils::source::snippet_with_applicability;
55
use if_chain::if_chain;
66
use rustc_errors::Applicability;
77
use rustc_hir as hir;
@@ -15,15 +15,15 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, recv: &h
1515
if is_trait_method(cx, expr, sym::Iterator);
1616
if let Some((Constant::Int(0), _)) = constant(cx, cx.typeck_results(), arg);
1717
then {
18-
let mut applicability = Applicability::MachineApplicable;
18+
let mut app = Applicability::MachineApplicable;
1919
span_lint_and_sugg(
2020
cx,
2121
ITER_NTH_ZERO,
2222
expr.span,
2323
"called `.nth(0)` on a `std::iter::Iterator`, when `.next()` is equivalent",
2424
"try calling `.next()` instead of `.nth(0)`",
25-
format!("{}.next()", snippet_with_applicability(cx, recv.span, "..", &mut applicability)),
26-
applicability,
25+
expr_sugg!(cx, &mut app, expr.span.ctxt(), $expr(recv).next()).into_string(),
26+
app,
2727
);
2828
}
2929
}

clippy_lints/src/methods/option_as_ref_deref.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use clippy_macros::expr_sugg;
12
use clippy_utils::diagnostics::span_lint_and_sugg;
23
use clippy_utils::source::snippet;
34
use clippy_utils::ty::is_type_diagnostic_item;
@@ -99,22 +100,15 @@ pub(super) fn check<'tcx>(
99100
format!(".as_ref().map({})", snippet(cx, map_arg.span, ".."))
100101
};
101102
let method_hint = if is_mut { "as_deref_mut" } else { "as_deref" };
102-
let hint = format!("{}.{}()", snippet(cx, as_ref_recv.span, ".."), method_hint);
103+
let mut app = Applicability::MachineApplicable;
104+
let hint = expr_sugg!(cx, &mut app, expr.span.ctxt(), $expr(as_ref_recv).$ident(method_hint)()).into_string();
103105
let suggestion = format!("try using {} instead", method_hint);
104106

105107
let msg = format!(
106108
"called `{0}` on an Option value. This can be done more directly \
107109
by calling `{1}` instead",
108110
current_method, hint
109111
);
110-
span_lint_and_sugg(
111-
cx,
112-
OPTION_AS_REF_DEREF,
113-
expr.span,
114-
&msg,
115-
&suggestion,
116-
hint,
117-
Applicability::MachineApplicable,
118-
);
112+
span_lint_and_sugg(cx, OPTION_AS_REF_DEREF, expr.span, &msg, &suggestion, hint, app);
119113
}
120114
}

clippy_lints/src/methods/option_map_or_none.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use clippy_macros::expr_sugg;
12
use clippy_utils::diagnostics::span_lint_and_sugg;
23
use clippy_utils::is_lang_ctor;
3-
use clippy_utils::source::snippet;
44
use clippy_utils::ty::is_type_diagnostic_item;
55
use rustc_errors::Applicability;
66
use rustc_hir as hir;
@@ -31,6 +31,7 @@ pub(super) fn check<'tcx>(
3131
return;
3232
}
3333

34+
let mut app = Applicability::MachineApplicable;
3435
let (lint_name, msg, instead, hint) = {
3536
let default_arg_is_none = if let hir::ExprKind::Path(ref qpath) = def_arg.kind {
3637
is_lang_ctor(cx, qpath, OptionNone)
@@ -50,39 +51,28 @@ pub(super) fn check<'tcx>(
5051
};
5152

5253
if is_option {
53-
let self_snippet = snippet(cx, recv.span, "..");
54-
let func_snippet = snippet(cx, map_arg.span, "..");
5554
let msg = "called `map_or(None, ..)` on an `Option` value. This can be done more directly by calling \
5655
`and_then(..)` instead";
5756
(
5857
OPTION_MAP_OR_NONE,
5958
msg,
6059
"try using `and_then` instead",
61-
format!("{0}.and_then({1})", self_snippet, func_snippet),
60+
expr_sugg!(cx, &mut app, expr.span.ctxt(), $expr(recv).and_then($expr(map_arg))).into_string(),
6261
)
6362
} else if f_arg_is_some {
6463
let msg = "called `map_or(None, Some)` on a `Result` value. This can be done more directly by calling \
6564
`ok()` instead";
66-
let self_snippet = snippet(cx, recv.span, "..");
6765
(
6866
RESULT_MAP_OR_INTO_OPTION,
6967
msg,
7068
"try using `ok` instead",
71-
format!("{0}.ok()", self_snippet),
69+
expr_sugg!(cx, &mut app, expr.span.ctxt(), $expr(recv).ok()).into_string(),
7270
)
7371
} else {
7472
// nothing to lint!
7573
return;
7674
}
7775
};
7876

79-
span_lint_and_sugg(
80-
cx,
81-
lint_name,
82-
expr.span,
83-
msg,
84-
instead,
85-
hint,
86-
Applicability::MachineApplicable,
87-
);
77+
span_lint_and_sugg(cx, lint_name, expr.span, msg, instead, hint, app);
8878
}

clippy_lints/src/methods/or_fun_call.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
use clippy_macros::expr_sugg;
12
use clippy_utils::diagnostics::span_lint_and_sugg;
23
use clippy_utils::eager_or_lazy::is_lazyness_candidate;
34
use clippy_utils::is_trait_item;
4-
use clippy_utils::source::{snippet, snippet_with_applicability, snippet_with_macro_callsite};
5+
use clippy_utils::source::{snippet, snippet_with_macro_callsite};
56
use clippy_utils::ty::implements_trait;
67
use clippy_utils::ty::{is_type_diagnostic_item, match_type};
78
use clippy_utils::{contains_return, last_path_segment, paths};
@@ -55,18 +56,15 @@ pub(super) fn check<'tcx>(
5556
|| (matches!(path, sym::new) && implements_default(arg, default_trait_id));
5657

5758
then {
58-
let mut applicability = Applicability::MachineApplicable;
59+
let mut app = Applicability::MachineApplicable;
5960
span_lint_and_sugg(
6061
cx,
6162
OR_FUN_CALL,
6263
span,
6364
&format!("use of `{}` followed by a call to `{}`", name, path),
6465
"try this",
65-
format!(
66-
"{}.unwrap_or_default()",
67-
snippet_with_applicability(cx, self_expr.span, "..", &mut applicability)
68-
),
69-
applicability,
66+
expr_sugg!(cx, &mut app, span.ctxt(), $expr(self_expr).unwrap_or_default()).into_string(),
67+
app,
7068
);
7169

7270
true

clippy_lints/src/methods/unwrap_or_else_default.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//! Lint for `some_result_or_option.unwrap_or_else(Default::default)`
22
33
use super::UNWRAP_OR_ELSE_DEFAULT;
4-
use clippy_utils::{
5-
diagnostics::span_lint_and_sugg, is_trait_item, source::snippet_with_applicability, ty::is_type_diagnostic_item,
6-
};
4+
use clippy_macros::expr_sugg;
5+
use clippy_utils::{diagnostics::span_lint_and_sugg, is_trait_item, ty::is_type_diagnostic_item};
76
use rustc_errors::Applicability;
87
use rustc_hir as hir;
98
use rustc_lint::LateContext;
@@ -26,19 +25,16 @@ pub(super) fn check<'tcx>(
2625
if is_option || is_result;
2726
if is_trait_item(cx, u_arg, sym::Default);
2827
then {
29-
let mut applicability = Applicability::MachineApplicable;
28+
let mut app = Applicability::MachineApplicable;
3029

3130
span_lint_and_sugg(
3231
cx,
3332
UNWRAP_OR_ELSE_DEFAULT,
3433
expr.span,
3534
"use of `.unwrap_or_else(..)` to construct default value",
3635
"try",
37-
format!(
38-
"{}.unwrap_or_default()",
39-
snippet_with_applicability(cx, recv.span, "..", &mut applicability)
40-
),
41-
applicability,
36+
expr_sugg!(cx, &mut app, expr.span.ctxt(), $expr(recv).unwrap_or_default()).into_string(),
37+
app,
4238
);
4339
}
4440
}

clippy_lints/src/strlen_on_c_strings.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl LateLintPass<'tcx> for StrlenOnCStrings {
6969
expr.span,
7070
"using `libc::strlen` on a `CString` or `CStr` value",
7171
"try this (you might also need to get rid of `unsafe` block in some cases):",
72-
expr_sugg!(cx, app, ctxt, $snip(self_arg).$ident(method_name)().len()).for_expr_position(cx, expr),
72+
expr_sugg!(cx, app, ctxt, $expr(self_arg).$ident(method_name)().len()).for_expr_position(cx, expr),
7373
Applicability::Unspecified // Sometimes unnecessary `unsafe` block
7474
);
7575
}

tests/ui/bytes_nth.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
error: called `.byte().nth()` on a `String`
1+
error: called `.bytes().nth()` on a `String`
22
--> $DIR/bytes_nth.rs:8:5
33
|
44
LL | s.bytes().nth(3);
55
| ^^^^^^^^^^^^^^^^ help: try: `s.as_bytes().get(3)`
66
|
77
= note: `-D clippy::bytes-nth` implied by `-D warnings`
88

9-
error: called `.byte().nth()` on a `String`
9+
error: called `.bytes().nth()` on a `String`
1010
--> $DIR/bytes_nth.rs:9:14
1111
|
1212
LL | let _ = &s.bytes().nth(3);
1313
| ^^^^^^^^^^^^^^^^ help: try: `s.as_bytes().get(3)`
1414

15-
error: called `.byte().nth()` on a `str`
15+
error: called `.bytes().nth()` on a `str`
1616
--> $DIR/bytes_nth.rs:10:5
1717
|
1818
LL | s[..].bytes().nth(3);

0 commit comments

Comments
 (0)