Skip to content

Commit 6e63b0d

Browse files
committed
Applicability-ify librustc_lint
Andrew Chin recently pointed out (rust-lang/cargo#5846) that it's surprising that `cargo fix` (now shipping with Cargo itself!) doesn't fix very common lint warnings, which is as good of a reminder as any that we should finish #50723.
1 parent 1d9405f commit 6e63b0d

File tree

4 files changed

+57
-19
lines changed

4 files changed

+57
-19
lines changed

src/librustc_lint/builtin.rs

+40-11
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for WhileTrue {
8484
let msg = "denote infinite loops with `loop { ... }`";
8585
let condition_span = cx.tcx.sess.codemap().def_span(e.span);
8686
let mut err = cx.struct_span_lint(WHILE_TRUE, condition_span, msg);
87-
err.span_suggestion_short(condition_span, "use `loop`", "loop".to_owned());
87+
err.span_suggestion_short_with_applicability(
88+
condition_span,
89+
"use `loop`",
90+
"loop".to_owned(),
91+
Applicability::MachineApplicable
92+
);
8893
err.emit();
8994
}
9095
}
@@ -191,7 +196,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonShorthandFieldPatterns {
191196
fieldpat.span,
192197
&format!("the `{}:` in this pattern is redundant", ident));
193198
let subspan = cx.tcx.sess.codemap().span_through_char(fieldpat.span, ':');
194-
err.span_suggestion_short(subspan, "remove this", ident.to_string());
199+
err.span_suggestion_short_with_applicability(
200+
subspan,
201+
"remove this",
202+
ident.to_string(),
203+
Applicability::MachineApplicable
204+
);
195205
err.emit();
196206
}
197207
}
@@ -708,10 +718,11 @@ impl EarlyLintPass for BadRepr {
708718
| "i8" | "i16" | "i32" | "i64" | "i128" | "isize" => {
709719
// if the literal could have been a valid `repr` arg,
710720
// suggest the correct syntax
711-
warn.span_suggestion(
721+
warn.span_suggestion_with_applicability(
712722
attr.span,
713723
"give `repr` a hint",
714724
repr_str(&lit.as_str()),
725+
Applicability::MachineApplicable
715726
);
716727
suggested = true;
717728
}
@@ -779,7 +790,12 @@ impl EarlyLintPass for DeprecatedAttr {
779790
let msg = format!("use of deprecated attribute `{}`: {}. See {}",
780791
name, reason, link);
781792
let mut err = cx.struct_span_lint(DEPRECATED, attr.span, &msg);
782-
err.span_suggestion_short(attr.span, "remove this attribute", "".to_owned());
793+
err.span_suggestion_short_with_applicability(
794+
attr.span,
795+
"remove this attribute",
796+
"".to_owned(),
797+
Applicability::MachineApplicable
798+
);
783799
err.emit();
784800
}
785801
return;
@@ -1201,7 +1217,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems {
12011217
}
12021218
};
12031219
if let Some(replacement) = suggestion {
1204-
err.span_suggestion(vis.span, "try making it public", replacement);
1220+
err.span_suggestion_with_applicability(
1221+
vis.span,
1222+
"try making it public",
1223+
replacement,
1224+
Applicability::MachineApplicable
1225+
);
12051226
}
12061227
};
12071228

@@ -1225,9 +1246,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems {
12251246
it.span,
12261247
"functions generic over \
12271248
types must be mangled");
1228-
err.span_suggestion_short(no_mangle_attr.span,
1229-
"remove this attribute",
1230-
"".to_owned());
1249+
err.span_suggestion_short_with_applicability(
1250+
no_mangle_attr.span,
1251+
"remove this attribute",
1252+
"".to_owned(),
1253+
// Use of `#[no_mangle]` suggests FFI intent; correct
1254+
// fix may be to monomorphize source by hand
1255+
Applicability::MaybeIncorrect
1256+
);
12311257
err.emit();
12321258
break;
12331259
}
@@ -1257,9 +1283,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems {
12571283
.unwrap_or(0) as u32;
12581284
// `const` is 5 chars
12591285
let const_span = it.span.with_hi(BytePos(it.span.lo().0 + start + 5));
1260-
err.span_suggestion(const_span,
1261-
"try a static value",
1262-
"pub static".to_owned());
1286+
err.span_suggestion_with_applicability(
1287+
const_span,
1288+
"try a static value",
1289+
"pub static".to_owned(),
1290+
Applicability::MachineApplicable
1291+
);
12631292
err.emit();
12641293
}
12651294
}

src/librustc_lint/types.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use std::cmp;
2222
use std::{i8, i16, i32, i64, u8, u16, u32, u64, f32, f64};
2323

2424
use syntax::{ast, attr};
25+
use syntax::errors::Applicability;
2526
use rustc_target::spec::abi::Abi;
2627
use syntax_pos::Span;
2728
use syntax::codemap;
@@ -143,9 +144,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeLimits {
143144
OVERFLOWING_LITERALS,
144145
parent_expr.span,
145146
"only u8 can be cast into char");
146-
err.span_suggestion(parent_expr.span,
147-
&"use a char literal instead",
148-
format!("'\\u{{{:X}}}'", lit_val));
147+
err.span_suggestion_with_applicability(
148+
parent_expr.span,
149+
&"use a char literal instead",
150+
format!("'\\u{{{:X}}}'", lit_val),
151+
Applicability::MachineApplicable
152+
);
149153
err.emit();
150154
return
151155
}
@@ -398,10 +402,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeLimits {
398402
{
399403
if let Some(pos) = repr_str.chars().position(|c| c == 'i' || c == 'u') {
400404
let (sans_suffix, _) = repr_str.split_at(pos);
401-
err.span_suggestion(
405+
err.span_suggestion_with_applicability(
402406
expr.span,
403407
&format!("consider using `{}` instead", sugg_ty),
404408
format!("{}{}", sans_suffix, sugg_ty),
409+
Applicability::MachineApplicable
405410
);
406411
} else {
407412
err.help(&format!("consider using `{}` instead", sugg_ty));

src/librustc_lint/unused.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use lint::{LintPass, EarlyLintPass, LateLintPass};
1717

1818
use syntax::ast;
1919
use syntax::attr;
20+
use syntax::errors::Applicability;
2021
use syntax::feature_gate::{BUILTIN_ATTRIBUTES, AttributeType};
2122
use syntax::print::pprust;
2223
use syntax::symbol::keywords;
@@ -303,9 +304,12 @@ impl UnusedParens {
303304
_ => false,
304305
}
305306
}).to_owned();
306-
err.span_suggestion_short(value.span,
307-
"remove these parentheses",
308-
parens_removed);
307+
err.span_suggestion_short_with_applicability(
308+
value.span,
309+
"remove these parentheses",
310+
parens_removed,
311+
Applicability::MachineApplicable
312+
);
309313
err.emit();
310314
}
311315
}

src/test/ui/lint/unused_parens_json_suggestion.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
],
8383
"label": null,
8484
"suggested_replacement": "1 / (2 + 3)",
85-
"suggestion_applicability": "Unspecified",
85+
"suggestion_applicability": "MachineApplicable",
8686
"expansion": null
8787
}
8888
],

0 commit comments

Comments
 (0)