1
Fork 0

suggest to remove prefix b in lint string

This commit is contained in:
csmoe 2018-10-09 17:53:59 +08:00
parent 42dde960f9
commit 2ed2d1a7e6
2 changed files with 92 additions and 45 deletions

View file

@ -1326,6 +1326,14 @@ impl LitKind {
} }
} }
/// Returns true if this literal is byte literal string false otherwise.
pub fn is_bytestr(&self) -> bool {
match self {
LitKind::ByteStr(_) => true,
_ => false,
}
}
/// Returns true if this is a numeric literal. /// Returns true if this is a numeric literal.
pub fn is_numeric(&self) -> bool { pub fn is_numeric(&self) -> bool {
match *self { match *self {

View file

@ -27,7 +27,7 @@ enum AttrError {
UnsupportedLiteral UnsupportedLiteral
} }
fn handle_errors(diag: &Handler, span: Span, error: AttrError) { fn handle_errors(diag: &Handler, span: Span, error: AttrError, is_bytestr: bool) {
match error { match error {
AttrError::MultipleItem(item) => span_err!(diag, span, E0538, AttrError::MultipleItem(item) => span_err!(diag, span, E0538,
"multiple '{}' items", item), "multiple '{}' items", item),
@ -44,7 +44,25 @@ fn handle_errors(diag: &Handler, span: Span, error: AttrError) {
AttrError::MissingFeature => span_err!(diag, span, E0546, "missing 'feature'"), AttrError::MissingFeature => span_err!(diag, span, E0546, "missing 'feature'"),
AttrError::MultipleStabilityLevels => span_err!(diag, span, E0544, AttrError::MultipleStabilityLevels => span_err!(diag, span, E0544,
"multiple stability levels"), "multiple stability levels"),
AttrError::UnsupportedLiteral => span_err!(diag, span, E0565, "unsupported literal"), AttrError::UnsupportedLiteral => {
let mut err = struct_span_err!(
diag,
span,
E0565,
"unsupported literal",
);
if is_bytestr {
if let Ok(lint_str) = sess.source_map.span_to_snippet(span) {
err.span_suggestion_with_applicability(
span,
"consider removing the prefix",
format!("{}", lint_str[1..]),
Applicability::MaybeIncorrect,
);
}
}
err.emit();
}
} }
} }
@ -202,7 +220,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
let meta = meta.as_ref().unwrap(); let meta = meta.as_ref().unwrap();
let get = |meta: &MetaItem, item: &mut Option<Symbol>| { let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
if item.is_some() { if item.is_some() {
handle_errors(diagnostic, meta.span, AttrError::MultipleItem(meta.name())); handle_errors(diagnostic, meta.span, AttrError::MultipleItem(meta.name()), false);
return false return false
} }
if let Some(v) = meta.value_str() { if let Some(v) = meta.value_str() {
@ -231,12 +249,14 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
handle_errors( handle_errors(
diagnostic, diagnostic,
mi.span, mi.span,
AttrError::UnknownMetaItem(mi.name(), expected)); AttrError::UnknownMetaItem(mi.name(), expected),
false
);
continue 'outer continue 'outer
} }
} }
} else { } else {
handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral); handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral, false);
continue 'outer continue 'outer
} }
} }
@ -261,7 +281,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
}) })
} }
(None, _) => { (None, _) => {
handle_errors(diagnostic, attr.span(), AttrError::MissingSince); handle_errors(diagnostic, attr.span(), AttrError::MissingSince, false);
continue continue
} }
_ => { _ => {
@ -287,7 +307,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
} }
"unstable" => { "unstable" => {
if stab.is_some() { if stab.is_some() {
handle_errors(diagnostic, attr.span(), AttrError::MultipleStabilityLevels); handle_errors(diagnostic, attr.span(), AttrError::MultipleStabilityLevels, false);
break break
} }
@ -308,12 +328,13 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
mi.name(), mi.name(),
&["feature", "reason", "issue"] &["feature", "reason", "issue"]
), ),
false,
); );
continue 'outer continue 'outer
} }
} }
} else { } else {
handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral); handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral, false);
continue 'outer continue 'outer
} }
} }
@ -340,7 +361,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
}) })
} }
(None, _, _) => { (None, _, _) => {
handle_errors(diagnostic, attr.span(), AttrError::MissingFeature); handle_errors(diagnostic, attr.span(), AttrError::MissingFeature, false);
continue continue
} }
_ => { _ => {
@ -351,14 +372,15 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
} }
"stable" => { "stable" => {
if stab.is_some() { if stab.is_some() {
handle_errors(diagnostic, attr.span(), AttrError::MultipleStabilityLevels); handle_errors(diagnostic, attr.span(), AttrError::MultipleStabilityLevels, false);
break break
} }
let mut feature = None; let mut feature = None;
let mut since = None; let mut since = None;
for meta in metas { for meta in metas {
if let NestedMetaItemKind::MetaItem(ref mi) = meta.node { match &meta.node {
NestedMetaItemKind::MetaItem(mi) => {
match &*mi.name().as_str() { match &*mi.name().as_str() {
"feature" => if !get(mi, &mut feature) { continue 'outer }, "feature" => if !get(mi, &mut feature) { continue 'outer },
"since" => if !get(mi, &mut since) { continue 'outer }, "since" => if !get(mi, &mut since) { continue 'outer },
@ -367,15 +389,22 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
diagnostic, diagnostic,
meta.span, meta.span,
AttrError::UnknownMetaItem(mi.name(), &["since", "note"]), AttrError::UnknownMetaItem(mi.name(), &["since", "note"]),
false,
); );
continue 'outer continue 'outer
} }
} }
} else { NestedMetaItemKind::Literal(lit) => {
handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral); handle_errors(
diagnostic,
meta.span,
AttrError::UnsupportedLiteral,
lit.node.is_bytestr()
);
continue 'outer continue 'outer
} }
} }
}
match (feature, since) { match (feature, since) {
(Some(feature), Some(since)) => { (Some(feature), Some(since)) => {
@ -390,11 +419,11 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
}) })
} }
(None, _) => { (None, _) => {
handle_errors(diagnostic, attr.span(), AttrError::MissingFeature); handle_errors(diagnostic, attr.span(), AttrError::MissingFeature, false);
continue continue
} }
_ => { _ => {
handle_errors(diagnostic, attr.span(), AttrError::MissingSince); handle_errors(diagnostic, attr.span(), AttrError::MissingSince, false);
continue continue
} }
} }
@ -461,8 +490,13 @@ pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Feat
MetaItemKind::List(..) => { MetaItemKind::List(..) => {
error(cfg.span, "unexpected parentheses after `cfg` predicate key") error(cfg.span, "unexpected parentheses after `cfg` predicate key")
} }
MetaItemKind::NameValue(lit) if !lit.node.is_str() => { MetaItemKind::NameValue(lit) => if !lit.node.is_str() {
error(lit.span, "literal in `cfg` predicate value must be a string") handle_errors(
&sess.span_diagnostic,
lit.span, AttrError::UnsupportedLiteral,
lit.node.is_bytestr(),
);
true
} }
MetaItemKind::NameValue(..) | MetaItemKind::Word => { MetaItemKind::NameValue(..) | MetaItemKind::Word => {
sess.config.contains(&(cfg.name(), cfg.value_str())) sess.config.contains(&(cfg.name(), cfg.value_str()))
@ -481,7 +515,7 @@ pub fn eval_condition<F>(cfg: &ast::MetaItem, sess: &ParseSess, eval: &mut F)
ast::MetaItemKind::List(ref mis) => { ast::MetaItemKind::List(ref mis) => {
for mi in mis.iter() { for mi in mis.iter() {
if !mi.is_meta_item() { if !mi.is_meta_item() {
handle_errors(&sess.span_diagnostic, mi.span, AttrError::UnsupportedLiteral); handle_errors(&sess.span_diagnostic, mi.span, AttrError::UnsupportedLiteral, false);
return false; return false;
} }
} }
@ -551,7 +585,7 @@ fn find_deprecation_generic<'a, I>(diagnostic: &Handler,
depr = if let Some(metas) = attr.meta_item_list() { depr = if let Some(metas) = attr.meta_item_list() {
let get = |meta: &MetaItem, item: &mut Option<Symbol>| { let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
if item.is_some() { if item.is_some() {
handle_errors(diagnostic, meta.span, AttrError::MultipleItem(meta.name())); handle_errors(diagnostic, meta.span, AttrError::MultipleItem(meta.name()), false);
return false return false
} }
if let Some(v) = meta.value_str() { if let Some(v) = meta.value_str() {
@ -566,7 +600,8 @@ fn find_deprecation_generic<'a, I>(diagnostic: &Handler,
let mut since = None; let mut since = None;
let mut note = None; let mut note = None;
for meta in metas { for meta in metas {
if let NestedMetaItemKind::MetaItem(ref mi) = meta.node { match &meta.node {
NestedMetaItemKind::MetaItem(mi) => {
match &*mi.name().as_str() { match &*mi.name().as_str() {
"since" => if !get(mi, &mut since) { continue 'outer }, "since" => if !get(mi, &mut since) { continue 'outer },
"note" => if !get(mi, &mut note) { continue 'outer }, "note" => if !get(mi, &mut note) { continue 'outer },
@ -575,15 +610,19 @@ fn find_deprecation_generic<'a, I>(diagnostic: &Handler,
diagnostic, diagnostic,
meta.span, meta.span,
AttrError::UnknownMetaItem(mi.name(), &["since", "note"]), AttrError::UnknownMetaItem(mi.name(), &["since", "note"]),
false,
); );
continue 'outer continue 'outer
} }
} }
} else { }
handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral); NestedMetaItemKind::Literal(lit) => {
let is_bytestr = lit.node.is_bytestr();
handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral, is_bytestr);
continue 'outer continue 'outer
} }
} }
}
Some(Deprecation {since: since, note: note}) Some(Deprecation {since: since, note: note})
} else { } else {
@ -638,7 +677,7 @@ pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec<ReprAttr>
mark_used(attr); mark_used(attr);
for item in items { for item in items {
if !item.is_meta_item() { if !item.is_meta_item() {
handle_errors(diagnostic, item.span, AttrError::UnsupportedLiteral); handle_errors(diagnostic, item.span, AttrError::UnsupportedLiteral, false);
continue continue
} }