Skip to content

Commit cb9e632

Browse files
committed
Add suggestion for negation of unsigned ints
1 parent f8c2d57 commit cb9e632

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/librustc_lint/types.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,19 @@ impl LateLintPass for TypeLimits {
104104
if let hir::ExprLit(ref lit) = expr.node {
105105
match lit.node {
106106
ast::LitInt(_, ast::UnsignedIntLit(_)) => {
107-
forbid_unsigned_negation(cx, e.span);
107+
forbid_unsigned_negation(cx, e.span, lit.span);
108108
},
109109
ast::LitInt(_, ast::UnsuffixedIntLit(_)) => {
110110
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);
112112
}
113113
},
114114
_ => ()
115115
}
116116
} else {
117117
let t = cx.tcx.node_id_to_type(expr.id);
118118
if let ty::TyUint(_) = t.sty {
119-
forbid_unsigned_negation(cx, e.span);
119+
forbid_unsigned_negation(cx, e.span, expr.span);
120120
}
121121
}
122122
// propagate negation, if the negation itself isn't negated
@@ -344,11 +344,17 @@ impl LateLintPass for TypeLimits {
344344
}
345345
}
346346

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();
352358
}
353359
}
354360
}

0 commit comments

Comments
 (0)