Skip to content

Commit

Permalink
Mark lints with applicability
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed May 4, 2018
1 parent 9d34e8d commit baa7b32
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
21 changes: 11 additions & 10 deletions src/librustc/lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//! compiler code, rather than using their own custom pass. Those
//! lints are all available in `rustc_lint::builtin`.
use errors::DiagnosticBuilder;
use errors::{Applicability, DiagnosticBuilder};
use lint::{LintPass, LateLintPass, LintArray};
use session::Session;
use syntax::codemap::Span;
Expand Down Expand Up @@ -341,15 +341,16 @@ impl BuiltinLintDiagnostics {
match self {
BuiltinLintDiagnostics::Normal => (),
BuiltinLintDiagnostics::BareTraitObject(span, is_global) => {
let sugg = match sess.codemap().span_to_snippet(span) {
Ok(ref s) if is_global => format!("dyn ({})", s),
Ok(s) => format!("dyn {}", s),
Err(_) => format!("dyn <type>")
let (sugg, app) = match sess.codemap().span_to_snippet(span) {
Ok(ref s) if is_global => (format!("dyn ({})", s),
Applicability::MachineApplicable),
Ok(s) => (format!("dyn {}", s), Applicability::MachineApplicable),
Err(_) => (format!("dyn <type>"), Applicability::HasPlaceholders)
};
db.span_suggestion(span, "use `dyn`", sugg);
db.span_suggestion_with_applicability(span, "use `dyn`", sugg, app);
}
BuiltinLintDiagnostics::AbsPathWithModule(span) => {
let sugg = match sess.codemap().span_to_snippet(span) {
let (sugg, app) = match sess.codemap().span_to_snippet(span) {
Ok(ref s) => {
// FIXME(Manishearth) ideally the emitting code
// can tell us whether or not this is global
Expand All @@ -359,11 +360,11 @@ impl BuiltinLintDiagnostics {
"::"
};

format!("crate{}{}", opt_colon, s)
(format!("crate{}{}", opt_colon, s), Applicability::MachineApplicable)
}
Err(_) => format!("crate::<path>")
Err(_) => (format!("crate::<path>"), Applicability::HasPlaceholders)
};
db.span_suggestion(span, "use `crate`", sugg);
db.span_suggestion_with_applicability(span, "use `crate`", sugg, app);
}
}
}
Expand Down
16 changes: 14 additions & 2 deletions src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use syntax::attr;
use syntax::feature_gate::{AttributeGate, AttributeType, Stability, deprecated_attributes};
use syntax_pos::{BytePos, Span, SyntaxContext};
use syntax::symbol::keywords;
use syntax::errors::DiagnosticBuilder;
use syntax::errors::{Applicability, DiagnosticBuilder};

use rustc::hir::{self, PatKind};
use rustc::hir::intravisit::FnKind;
Expand Down Expand Up @@ -1300,7 +1300,19 @@ impl UnreachablePub {
} else {
"pub(crate)"
}.to_owned();
err.span_suggestion(pub_span, "consider restricting its visibility", replacement);
let app = if span.ctxt().outer().expn_info().is_none() {
// even if macros aren't involved the suggestion
// may be incorrect -- the user may have mistakenly
// hidden it behind a private module and this lint is
// a helpful way to catch that. However, we're trying
// not to change the nature of the code with this lint
// so it's marked as machine applicable.
Applicability::MachineApplicable
} else {
Applicability::MaybeIncorrect
};
err.span_suggestion_with_applicability(pub_span, "consider restricting its visibility",
replacement, app);
if exportable {
err.help("or consider exporting it for use by other crates");
}
Expand Down

0 comments on commit baa7b32

Please sign in to comment.