@@ -104,19 +104,19 @@ impl LateLintPass for TypeLimits {
104
104
if let hir:: ExprLit ( ref lit) = expr. node {
105
105
match lit. node {
106
106
ast:: LitInt ( _, ast:: UnsignedIntLit ( _) ) => {
107
- forbid_unsigned_negation ( cx, e. span ) ;
107
+ forbid_unsigned_negation ( cx, e. span , lit . span ) ;
108
108
} ,
109
109
ast:: LitInt ( _, ast:: UnsuffixedIntLit ( _) ) => {
110
110
if let ty:: TyUint ( _) = cx. tcx . node_id_to_type ( e. id ) . sty {
111
- forbid_unsigned_negation ( cx, e. span ) ;
111
+ forbid_unsigned_negation ( cx, e. span , lit . span ) ;
112
112
}
113
113
} ,
114
114
_ => ( )
115
115
}
116
116
} else {
117
117
let t = cx. tcx . node_id_to_type ( expr. id ) ;
118
118
if let ty:: TyUint ( _) = t. sty {
119
- forbid_unsigned_negation ( cx, e. span ) ;
119
+ forbid_unsigned_negation ( cx, e. span , expr . span ) ;
120
120
}
121
121
}
122
122
// propagate negation, if the negation itself isn't negated
@@ -344,11 +344,17 @@ impl LateLintPass for TypeLimits {
344
344
}
345
345
}
346
346
347
- fn forbid_unsigned_negation ( cx : & LateContext , span : Span ) {
348
- cx. sess ( )
349
- . struct_span_err_with_code ( span, "unary negation of unsigned integer" , "E0519" )
350
- . span_help ( span, "use a cast or the `!` operator" )
351
- . emit ( ) ;
347
+ fn forbid_unsigned_negation ( cx : & LateContext , span : Span , lit_span : Span ) {
348
+ let mut err = cx. sess ( )
349
+ . struct_span_err_with_code ( span,
350
+ "unary negation of unsigned integer" ,
351
+ "E0519" ) ;
352
+ if let Ok ( snip) = cx. sess ( ) . codemap ( ) . span_to_snippet ( lit_span) {
353
+ err. span_suggestion ( span, "try using a cast or the `!` operator:" , snip) ;
354
+ } else {
355
+ err. span_help ( span, "use a cast or the `!` operator" ) ;
356
+ }
357
+ err. emit ( ) ;
352
358
}
353
359
}
354
360
}
0 commit comments