@@ -6,7 +6,9 @@ use rustc_lint::LateContext;
6
6
use super :: UNNECESSARY_LITERAL_UNWRAP ;
7
7
8
8
pub ( super ) fn check ( cx : & LateContext < ' _ > , expr : & hir:: Expr < ' _ > , recv : & hir:: Expr < ' _ > , name : & str ) {
9
- if let hir:: ExprKind :: Call ( call, [ arg] ) = recv. kind {
9
+ let init = clippy_utils:: expr_or_init ( cx, recv) ;
10
+
11
+ if let hir:: ExprKind :: Call ( call, [ arg] ) = init. kind {
10
12
let mess = if is_res_lang_ctor ( cx, path_res ( cx, call) , hir:: LangItem :: OptionSome ) {
11
13
Some ( "Some" )
12
14
} else if is_res_lang_ctor ( cx, path_res ( cx, call) , hir:: LangItem :: ResultOk ) {
@@ -15,15 +17,19 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr
15
17
None
16
18
} ;
17
19
18
- if let Some ( constructor) = mess {
20
+ let Some ( constructor) = mess else {
21
+ return ;
22
+ } ;
23
+
24
+ if init. span == recv. span {
19
25
span_lint_and_then (
20
26
cx,
21
27
UNNECESSARY_LITERAL_UNWRAP ,
22
28
expr. span ,
23
29
& format ! ( "used `{name}()` on `{constructor}` value" ) ,
24
30
|diag| {
25
31
let suggestions = vec ! [
26
- ( call . span. with_hi( arg. span. lo( ) ) , String :: new( ) ) ,
32
+ ( recv . span. with_hi( arg. span. lo( ) ) , String :: new( ) ) ,
27
33
( expr. span. with_lo( arg. span. hi( ) ) , String :: new( ) ) ,
28
34
] ;
29
35
@@ -34,6 +40,16 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr
34
40
) ;
35
41
} ,
36
42
) ;
43
+ } else {
44
+ span_lint_and_then (
45
+ cx,
46
+ UNNECESSARY_LITERAL_UNWRAP ,
47
+ expr. span ,
48
+ & format ! ( "used `{name}()` on `{constructor}` value" ) ,
49
+ |diag| {
50
+ diag. span_help ( init. span , format ! ( "remove the `{constructor}` and `{name}()`" ) ) ;
51
+ } ,
52
+ ) ;
37
53
}
38
54
}
39
55
}
0 commit comments