Skip to content

Commit 18f0991

Browse files
committed
Implement checks to the expression
The implemented checks are for checking if the expression is either of type `Option` and isn't a syntactical place
1 parent a5907a5 commit 18f0991

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

clippy_lints/src/methods/option_take_on_temporary.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ use clippy_utils::diagnostics::span_lint;
22
use clippy_utils::ty::is_type_diagnostic_item;
33
use rustc_hir::Expr;
44
use rustc_lint::LateContext;
5-
use rustc_span::sym::Result as sym_result;
5+
use rustc_span::sym;
66

77
use super::OPTION_TAKE_ON_TEMPORARY;
88

9-
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &'tcx Expr<'_>) {
10-
if_chain! {
11-
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym_result);
12-
let result_type = cx.typeck_results().expr_ty(recv);
13-
9+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, _recv: &'tcx Expr<'_>) {
10+
// Checks if expression type is equal to sym::Option and if the expr is not a syntactic place
11+
if is_expr_option(cx, expr) & !expr.is_syntactic_place_expr() {
12+
span_lint(cx, OPTION_TAKE_ON_TEMPORARY, expr.span, "Format test");
13+
}
14+
/* if_chain! {
15+
is_expr_option(cx, expr);
1416
then {
1517
span_lint(
1618
cx,
@@ -19,5 +21,10 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &'
1921
"Format test"
2022
);
2123
}
22-
};
24+
};*/
25+
}
26+
27+
fn is_expr_option(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
28+
let expr_type = cx.typeck_results().expr_ty(expr);
29+
is_type_diagnostic_item(cx, expr_type, sym::Option)
2330
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: Format test
2+
--> $DIR/option_take_on_temporary.rs:4:13
3+
|
4+
LL | let y = x.as_ref().take();
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::option-take-on-temporary` implied by `-D warnings`
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)