@@ -10,11 +10,36 @@ use crate::errors;
10
10
pub fn maybe_expr_static_mut ( tcx : TyCtxt < ' _ > , expr : hir:: Expr < ' _ > ) {
11
11
let span = expr. span ;
12
12
let hir_id = expr. hir_id ;
13
- if let hir:: ExprKind :: AddrOf ( borrow_kind, m, expr) = expr. kind
14
- && matches ! ( borrow_kind, hir:: BorrowKind :: Ref )
15
- && let Some ( var) = path_if_static_mut ( tcx, expr)
16
- {
17
- handle_static_mut_ref ( tcx, span, var, span. edition ( ) . at_least_rust_2024 ( ) , m, hir_id) ;
13
+ match expr. kind {
14
+ hir:: ExprKind :: AddrOf ( borrow_kind, m, expr) => {
15
+ if matches ! ( borrow_kind, hir:: BorrowKind :: Ref )
16
+ && let Some ( var) = path_if_static_mut ( tcx, expr)
17
+ {
18
+ handle_static_mut_ref (
19
+ tcx,
20
+ span,
21
+ var,
22
+ span. edition ( ) . at_least_rust_2024 ( ) ,
23
+ m,
24
+ hir_id,
25
+ true ,
26
+ ) ;
27
+ }
28
+ }
29
+ hir:: ExprKind :: MethodCall ( _, expr, _, _) | hir:: ExprKind :: Index ( expr, _, _) => {
30
+ if let Some ( var) = path_if_static_mut ( tcx, expr) {
31
+ handle_static_mut_ref (
32
+ tcx,
33
+ span,
34
+ var,
35
+ span. edition ( ) . at_least_rust_2024 ( ) ,
36
+ Mutability :: Not ,
37
+ hir_id,
38
+ false ,
39
+ ) ;
40
+ }
41
+ }
42
+ _ => { }
18
43
}
19
44
}
20
45
@@ -33,6 +58,7 @@ pub fn maybe_stmt_static_mut(tcx: TyCtxt<'_>, stmt: hir::Stmt<'_>) {
33
58
loc. span . edition ( ) . at_least_rust_2024 ( ) ,
34
59
rmutbl,
35
60
stmt. hir_id ,
61
+ true ,
36
62
) ;
37
63
}
38
64
}
@@ -55,25 +81,42 @@ fn handle_static_mut_ref(
55
81
e2024 : bool ,
56
82
mutable : Mutability ,
57
83
hir_id : hir:: HirId ,
84
+ suggest : bool ,
58
85
) {
86
+ let shared = if mutable == Mutability :: Mut { "mutable" } else { "shared" } ;
59
87
if e2024 {
60
- let ( sugg, shared) = if mutable == Mutability :: Mut {
61
- ( errors:: StaticMutRefSugg :: Mut { span, var } , "mutable" )
62
- } else {
63
- ( errors:: StaticMutRefSugg :: Shared { span, var } , "shared" )
64
- } ;
65
- tcx. sess . psess . dcx . emit_err ( errors:: StaticMutRef { span, sugg, shared } ) ;
66
- } else {
67
- let ( sugg, shared) = if mutable == Mutability :: Mut {
68
- ( errors:: RefOfMutStaticSugg :: Mut { span, var } , "mutable" )
88
+ if suggest {
89
+ let sugg = if mutable == Mutability :: Mut {
90
+ errors:: StaticMutRefSugg :: Mut { span, var }
91
+ } else {
92
+ errors:: StaticMutRefSugg :: Shared { span, var }
93
+ } ;
94
+ tcx. sess . psess . dcx . emit_err ( errors:: StaticMutRef { span, sugg : Some ( sugg) , shared } ) ;
95
+ return ;
96
+ }
97
+ tcx. sess . psess . dcx . emit_err ( errors:: StaticMutRef { span, sugg : None , shared } ) ;
98
+ return ;
99
+ }
100
+
101
+ if suggest {
102
+ let sugg = if mutable == Mutability :: Mut {
103
+ errors:: RefOfMutStaticSugg :: Mut { span, var }
69
104
} else {
70
- ( errors:: RefOfMutStaticSugg :: Shared { span, var } , "shared" )
105
+ errors:: RefOfMutStaticSugg :: Shared { span, var }
71
106
} ;
72
107
tcx. emit_node_span_lint (
73
108
STATIC_MUT_REFS ,
74
109
hir_id,
75
110
span,
76
- errors:: RefOfMutStatic { span, sugg, shared } ,
111
+ errors:: RefOfMutStatic { span, sugg : Some ( sugg ) , shared } ,
77
112
) ;
113
+ return ;
78
114
}
115
+
116
+ tcx. emit_node_span_lint (
117
+ STATIC_MUT_REFS ,
118
+ hir_id,
119
+ span,
120
+ errors:: RefOfMutStatic { span, sugg : None , shared } ,
121
+ ) ;
79
122
}
0 commit comments