Skip to content

Commit e72e9d9

Browse files
committed
Swap span_lint for span_lint_and_sugg
This implements a machine applicable suggestion to any matched usage of `.as_ref().take()``
1 parent 18f0991 commit e72e9d9

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

clippy_lints/src/methods/option_take_on_temporary.rs

+17-14
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1-
use clippy_utils::diagnostics::span_lint;
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::source::snippet_with_applicability;
23
use clippy_utils::ty::is_type_diagnostic_item;
4+
use rustc_errors::Applicability;
35
use rustc_hir::Expr;
46
use rustc_lint::LateContext;
57
use rustc_span::sym;
68

79
use super::OPTION_TAKE_ON_TEMPORARY;
810

9-
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, _recv: &'tcx Expr<'_>) {
11+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &'tcx Expr<'_>) {
1012
// Checks if expression type is equal to sym::Option and if the expr is not a syntactic place
1113
if is_expr_option(cx, expr) & !expr.is_syntactic_place_expr() {
12-
span_lint(cx, OPTION_TAKE_ON_TEMPORARY, expr.span, "Format test");
14+
let mut applicability = Applicability::MachineApplicable;
15+
span_lint_and_sugg(
16+
cx,
17+
OPTION_TAKE_ON_TEMPORARY,
18+
expr.span,
19+
"Called `Option::take()` on a temporary value",
20+
"try",
21+
format!(
22+
"{}",
23+
snippet_with_applicability(cx, recv.span, "..", &mut applicability)
24+
),
25+
Applicability::MachineApplicable,
26+
);
1327
}
14-
/* if_chain! {
15-
is_expr_option(cx, expr);
16-
then {
17-
span_lint(
18-
cx,
19-
OPTION_TAKE_ON_TEMPORARY,
20-
expr.span,
21-
"Format test"
22-
);
23-
}
24-
};*/
2528
}
2629

2730
fn is_expr_option(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// run-rustfix
2+
3+
fn main() {
4+
println!("Testing option_take_on_temporary");
5+
let x = Some(3);
6+
let y = x.as_ref();
7+
}

tests/ui/option_take_on_temporary.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// run-rustfix
2+
13
fn main() {
24
println!("Testing option_take_on_temporary");
35
let x = Some(3);

tests/ui/option_take_on_temporary.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Format test
2-
--> $DIR/option_take_on_temporary.rs:4:13
1+
error: Called `Option::take()` on a temporary value
2+
--> $DIR/option_take_on_temporary.rs:6:13
33
|
44
LL | let y = x.as_ref().take();
5-
| ^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^ help: try: `x.as_ref()`
66
|
77
= note: `-D clippy::option-take-on-temporary` implied by `-D warnings`
88

0 commit comments

Comments
 (0)