Skip to content

Commit f8f1441

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 ee9281d commit f8f1441

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

clippy_lints/src/methods/needless_option_take.rs

Lines changed: 17 additions & 14 deletions
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::NEEDLESS_OPTION_TAKE;
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+
NEEDLESS_OPTION_TAKE,
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,
26+
);
1327
}
14-
/* if_chain! {
15-
is_expr_option(cx, expr);
16-
then {
17-
span_lint(
18-
cx,
19-
NEEDLESS_OPTION_TAKE,
20-
expr.span,
21-
"Format test"
22-
);
23-
}
24-
};*/
2528
}
2629

2730
fn is_expr_option(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {

tests/ui/needless_option_take.rs

Lines changed: 2 additions & 0 deletions
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);
Lines changed: 7 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)