Suggest valid crate type if invalid

This adds a suggestion to the `invalid_crate_types` lint.

The suggestion is based on the Levenshtein distance to existing crate
types. If no suggestion is found it will show the lint without any
suggestions.
This commit is contained in:
Philipp Hansch 2018-09-13 08:42:19 +02:00
parent d8af8b66d9
commit 7249a1b1ae
No known key found for this signature in database
GPG Key ID: B6FA06A6E0E2665B
4 changed files with 143 additions and 10 deletions

View File

@ -422,6 +422,7 @@ pub enum BuiltinLintDiagnostics {
ProcMacroDeriveResolutionFallback(Span),
MacroExpandedMacroExportsAccessedByAbsolutePaths(Span),
ElidedLifetimesInPaths(usize, Span, bool, Span, String),
UnknownCrateTypes(Span, String, String),
}
impl BuiltinLintDiagnostics {
@ -500,6 +501,14 @@ impl BuiltinLintDiagnostics {
Applicability::MachineApplicable
);
}
BuiltinLintDiagnostics::UnknownCrateTypes(span, note, sugg) => {
db.span_suggestion_with_applicability(
span,
&note,
sugg,
Applicability::MaybeIncorrect
);
}
}
}
}

View File

@ -57,6 +57,8 @@ use syntax::ext::base::ExtCtxt;
use syntax::fold::Folder;
use syntax::parse::{self, PResult};
use syntax::util::node_count::NodeCounter;
use syntax::util::lev_distance::find_best_match_for_name;
use syntax::symbol::Symbol;
use syntax_pos::{FileName, hygiene};
use syntax_ext;
@ -1508,13 +1510,45 @@ pub fn collect_crate_types(session: &Session, attrs: &[ast::Attribute]) -> Vec<c
Some(ref n) if *n == "staticlib" => Some(config::CrateType::Staticlib),
Some(ref n) if *n == "proc-macro" => Some(config::CrateType::ProcMacro),
Some(ref n) if *n == "bin" => Some(config::CrateType::Executable),
Some(_) => {
session.buffer_lint(
lint::builtin::UNKNOWN_CRATE_TYPES,
ast::CRATE_NODE_ID,
a.span,
"invalid `crate_type` value",
);
Some(ref n) => {
let crate_types = vec![
Symbol::intern("rlib"),
Symbol::intern("dylib"),
Symbol::intern("cdylib"),
Symbol::intern("lib"),
Symbol::intern("staticlib"),
Symbol::intern("proc-macro"),
Symbol::intern("bin")
];
if let ast::MetaItemKind::NameValue(spanned) = a.meta().unwrap().node {
let span = spanned.span;
let lev_candidate = find_best_match_for_name(
crate_types.iter(),
&n.as_str(),
None
);
if let Some(candidate) = lev_candidate {
session.buffer_lint_with_diagnostic(
lint::builtin::UNKNOWN_CRATE_TYPES,
ast::CRATE_NODE_ID,
span,
"invalid `crate_type` value",
lint::builtin::BuiltinLintDiagnostics::
UnknownCrateTypes(
span,
"did you mean".to_string(),
format!("\"{}\"", candidate)
)
);
} else {
session.buffer_lint(
lint::builtin::UNKNOWN_CRATE_TYPES,
ast::CRATE_NODE_ID,
span,
"invalid `crate_type` value"
);
}
}
None
}
_ => {

View File

@ -11,6 +11,48 @@
// regression test for issue 11256
#![crate_type="foo"] //~ ERROR invalid `crate_type` value
// Tests for suggestions (#53958)
#![crate_type="statoclib"]
//~^ ERROR invalid `crate_type` value
//~| HELP did you mean
//~| SUGGESTION staticlib
#![crate_type="procmacro"]
//~^ ERROR invalid `crate_type` value
//~| HELP did you mean
//~| SUGGESTION proc-macro
#![crate_type="static-lib"]
//~^ ERROR invalid `crate_type` value
//~| HELP did you mean
//~| SUGGESTION staticlib
#![crate_type="drylib"]
//~^ ERROR invalid `crate_type` value
//~| HELP did you mean
//~| SUGGESTION dylib
#![crate_type="dlib"]
//~^ ERROR invalid `crate_type` value
//~| HELP did you mean
//~| SUGGESTION rlib
#![crate_type="lob"]
//~^ ERROR invalid `crate_type` value
//~| HELP did you mean
//~| SUGGESTION lib
#![crate_type="bon"]
//~^ ERROR invalid `crate_type` value
//~| HELP did you mean
//~| SUGGESTION bin
#![crate_type="cdalib"]
//~^ ERROR invalid `crate_type` value
//~| HELP did you mean
//~| SUGGESTION cdylib
fn main() {
return
}

View File

@ -1,10 +1,58 @@
error: invalid `crate_type` value
--> $DIR/invalid-crate-type.rs:12:1
--> $DIR/invalid-crate-type.rs:12:15
|
LL | #![crate_type="foo"] //~ ERROR invalid `crate_type` value
| ^^^^^^^^^^^^^^^^^^^^
| ^^^^^
|
= note: #[deny(unknown_crate_types)] on by default
error: aborting due to previous error
error: invalid `crate_type` value
--> $DIR/invalid-crate-type.rs:16:15
|
LL | #![crate_type="statoclib"]
| ^^^^^^^^^^^ help: did you mean: `"staticlib"`
error: invalid `crate_type` value
--> $DIR/invalid-crate-type.rs:21:15
|
LL | #![crate_type="procmacro"]
| ^^^^^^^^^^^ help: did you mean: `"proc-macro"`
error: invalid `crate_type` value
--> $DIR/invalid-crate-type.rs:26:15
|
LL | #![crate_type="static-lib"]
| ^^^^^^^^^^^^ help: did you mean: `"staticlib"`
error: invalid `crate_type` value
--> $DIR/invalid-crate-type.rs:31:15
|
LL | #![crate_type="drylib"]
| ^^^^^^^^ help: did you mean: `"dylib"`
error: invalid `crate_type` value
--> $DIR/invalid-crate-type.rs:36:15
|
LL | #![crate_type="dlib"]
| ^^^^^^ help: did you mean: `"rlib"`
error: invalid `crate_type` value
--> $DIR/invalid-crate-type.rs:41:15
|
LL | #![crate_type="lob"]
| ^^^^^ help: did you mean: `"lib"`
error: invalid `crate_type` value
--> $DIR/invalid-crate-type.rs:46:15
|
LL | #![crate_type="bon"]
| ^^^^^ help: did you mean: `"bin"`
error: invalid `crate_type` value
--> $DIR/invalid-crate-type.rs:51:15
|
LL | #![crate_type="cdalib"]
| ^^^^^^^^ help: did you mean: `"cdylib"`
error: aborting due to 9 previous errors