Skip to content

Commit

Permalink
Applicability-ify librustc_lint
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zackmdavis committed Aug 2, 2018
1 parent 1d9405f commit 6e63b0d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 19 deletions.
51 changes: 40 additions & 11 deletions src/librustc_lint/builtin.rs
Expand Up @@ -84,7 +84,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for WhileTrue {
let msg = "denote infinite loops with `loop { ... }`";
let condition_span = cx.tcx.sess.codemap().def_span(e.span);
let mut err = cx.struct_span_lint(WHILE_TRUE, condition_span, msg);
err.span_suggestion_short(condition_span, "use `loop`", "loop".to_owned());
err.span_suggestion_short_with_applicability(
condition_span,
"use `loop`",
"loop".to_owned(),
Applicability::MachineApplicable
);
err.emit();
}
}
Expand Down Expand Up @@ -191,7 +196,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonShorthandFieldPatterns {
fieldpat.span,
&format!("the `{}:` in this pattern is redundant", ident));
let subspan = cx.tcx.sess.codemap().span_through_char(fieldpat.span, ':');
err.span_suggestion_short(subspan, "remove this", ident.to_string());
err.span_suggestion_short_with_applicability(
subspan,
"remove this",
ident.to_string(),
Applicability::MachineApplicable
);
err.emit();
}
}
Expand Down Expand Up @@ -708,10 +718,11 @@ impl EarlyLintPass for BadRepr {
| "i8" | "i16" | "i32" | "i64" | "i128" | "isize" => {
// if the literal could have been a valid `repr` arg,
// suggest the correct syntax
warn.span_suggestion(
warn.span_suggestion_with_applicability(
attr.span,
"give `repr` a hint",
repr_str(&lit.as_str()),
Applicability::MachineApplicable
);
suggested = true;
}
Expand Down Expand Up @@ -779,7 +790,12 @@ impl EarlyLintPass for DeprecatedAttr {
let msg = format!("use of deprecated attribute `{}`: {}. See {}",
name, reason, link);
let mut err = cx.struct_span_lint(DEPRECATED, attr.span, &msg);
err.span_suggestion_short(attr.span, "remove this attribute", "".to_owned());
err.span_suggestion_short_with_applicability(
attr.span,
"remove this attribute",
"".to_owned(),
Applicability::MachineApplicable
);
err.emit();
}
return;
Expand Down Expand Up @@ -1201,7 +1217,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems {
}
};
if let Some(replacement) = suggestion {
err.span_suggestion(vis.span, "try making it public", replacement);
err.span_suggestion_with_applicability(
vis.span,
"try making it public",
replacement,
Applicability::MachineApplicable
);
}
};

Expand All @@ -1225,9 +1246,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems {
it.span,
"functions generic over \
types must be mangled");
err.span_suggestion_short(no_mangle_attr.span,
"remove this attribute",
"".to_owned());
err.span_suggestion_short_with_applicability(
no_mangle_attr.span,
"remove this attribute",
"".to_owned(),
// Use of `#[no_mangle]` suggests FFI intent; correct
// fix may be to monomorphize source by hand
Applicability::MaybeIncorrect
);
err.emit();
break;
}
Expand Down Expand Up @@ -1257,9 +1283,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems {
.unwrap_or(0) as u32;
// `const` is 5 chars
let const_span = it.span.with_hi(BytePos(it.span.lo().0 + start + 5));
err.span_suggestion(const_span,
"try a static value",
"pub static".to_owned());
err.span_suggestion_with_applicability(
const_span,
"try a static value",
"pub static".to_owned(),
Applicability::MachineApplicable
);
err.emit();
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/librustc_lint/types.rs
Expand Up @@ -22,6 +22,7 @@ use std::cmp;
use std::{i8, i16, i32, i64, u8, u16, u32, u64, f32, f64};

use syntax::{ast, attr};
use syntax::errors::Applicability;
use rustc_target::spec::abi::Abi;
use syntax_pos::Span;
use syntax::codemap;
Expand Down Expand Up @@ -143,9 +144,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeLimits {
OVERFLOWING_LITERALS,
parent_expr.span,
"only u8 can be cast into char");
err.span_suggestion(parent_expr.span,
&"use a char literal instead",
format!("'\\u{{{:X}}}'", lit_val));
err.span_suggestion_with_applicability(
parent_expr.span,
&"use a char literal instead",
format!("'\\u{{{:X}}}'", lit_val),
Applicability::MachineApplicable
);
err.emit();
return
}
Expand Down Expand Up @@ -398,10 +402,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeLimits {
{
if let Some(pos) = repr_str.chars().position(|c| c == 'i' || c == 'u') {
let (sans_suffix, _) = repr_str.split_at(pos);
err.span_suggestion(
err.span_suggestion_with_applicability(
expr.span,
&format!("consider using `{}` instead", sugg_ty),
format!("{}{}", sans_suffix, sugg_ty),
Applicability::MachineApplicable
);
} else {
err.help(&format!("consider using `{}` instead", sugg_ty));
Expand Down
10 changes: 7 additions & 3 deletions src/librustc_lint/unused.rs
Expand Up @@ -17,6 +17,7 @@ use lint::{LintPass, EarlyLintPass, LateLintPass};

use syntax::ast;
use syntax::attr;
use syntax::errors::Applicability;
use syntax::feature_gate::{BUILTIN_ATTRIBUTES, AttributeType};
use syntax::print::pprust;
use syntax::symbol::keywords;
Expand Down Expand Up @@ -303,9 +304,12 @@ impl UnusedParens {
_ => false,
}
}).to_owned();
err.span_suggestion_short(value.span,
"remove these parentheses",
parens_removed);
err.span_suggestion_short_with_applicability(
value.span,
"remove these parentheses",
parens_removed,
Applicability::MachineApplicable
);
err.emit();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/lint/unused_parens_json_suggestion.stderr
Expand Up @@ -82,7 +82,7 @@
],
"label": null,
"suggested_replacement": "1 / (2 + 3)",
"suggestion_applicability": "Unspecified",
"suggestion_applicability": "MachineApplicable",
"expansion": null
}
],
Expand Down

0 comments on commit 6e63b0d

Please sign in to comment.