Auto merge of #54929 - csmoe:cfg_lint, r=petrochenkov

Suggest to remove prefix `b` in cfg attribute lint string

Closes #54926
r? @estebank
This commit is contained in:
bors 2018-10-26 21:46:13 +00:00
commit fa45602b71
15 changed files with 221 additions and 78 deletions

View file

@ -134,11 +134,11 @@ impl<'a, 'tcx: 'a> Annotator<'a, 'tcx> {
if self.tcx.features().staged_api {
// This crate explicitly wants staged API.
debug!("annotate(id = {:?}, attrs = {:?})", id, attrs);
if let Some(..) = attr::find_deprecation(self.tcx.sess.diagnostic(), attrs, item_sp) {
if let Some(..) = attr::find_deprecation(&self.tcx.sess.parse_sess, attrs, item_sp) {
self.tcx.sess.span_err(item_sp, "`#[deprecated]` cannot be used in staged api, \
use `#[rustc_deprecated]` instead");
}
if let Some(mut stab) = attr::find_stability(self.tcx.sess.diagnostic(),
if let Some(mut stab) = attr::find_stability(&self.tcx.sess.parse_sess,
attrs, item_sp) {
// Error if prohibited, or can't inherit anything from a container
if kind == AnnotationKind::Prohibited ||
@ -224,7 +224,7 @@ impl<'a, 'tcx: 'a> Annotator<'a, 'tcx> {
}
}
if let Some(depr) = attr::find_deprecation(self.tcx.sess.diagnostic(), attrs, item_sp) {
if let Some(depr) = attr::find_deprecation(&self.tcx.sess.parse_sess, attrs, item_sp) {
if kind == AnnotationKind::Prohibited {
self.tcx.sess.span_err(item_sp, "This deprecation annotation is useless");
}

View file

@ -1928,7 +1928,7 @@ impl ReprOptions {
let mut max_align = 0;
let mut min_pack = 0;
for attr in tcx.get_attrs(did).iter() {
for r in attr::find_repr_attrs(tcx.sess.diagnostic(), attr) {
for r in attr::find_repr_attrs(&tcx.sess.parse_sess, attr) {
flags.insert(match r {
attr::ReprC => ReprFlags::IS_C,
attr::ReprPacked(pack) => {

View file

@ -121,7 +121,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonCamelCaseTypes {
let has_repr_c = it.attrs
.iter()
.any(|attr| {
attr::find_repr_attrs(cx.tcx.sess.diagnostic(), attr)
attr::find_repr_attrs(&cx.tcx.sess.parse_sess, attr)
.iter()
.any(|r| r == &attr::ReprC)
});

View file

@ -1713,7 +1713,7 @@ fn check_packed<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, sp: Span, def_id: DefId)
let repr = tcx.adt_def(def_id).repr;
if repr.packed() {
for attr in tcx.get_attrs(def_id).iter() {
for r in attr::find_repr_attrs(tcx.sess.diagnostic(), attr) {
for r in attr::find_repr_attrs(&tcx.sess.parse_sess, attr) {
if let attr::ReprPacked(pack) = r {
if pack != repr.pack {
struct_span_err!(tcx.sess, sp, E0634,

View file

@ -1328,6 +1328,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.
pub fn is_numeric(&self) -> bool {
match *self {

View file

@ -24,10 +24,11 @@ enum AttrError {
MissingSince,
MissingFeature,
MultipleStabilityLevels,
UnsupportedLiteral
UnsupportedLiteral(&'static str, /* is_bytestr */ bool),
}
fn handle_errors(diag: &Handler, span: Span, error: AttrError) {
fn handle_errors(sess: &ParseSess, span: Span, error: AttrError) {
let diag = &sess.span_diagnostic;
match error {
AttrError::MultipleItem(item) => span_err!(diag, span, E0538,
"multiple '{}' items", item),
@ -44,7 +45,23 @@ fn handle_errors(diag: &Handler, span: Span, error: AttrError) {
AttrError::MissingFeature => span_err!(diag, span, E0546, "missing 'feature'"),
AttrError::MultipleStabilityLevels => span_err!(diag, span, E0544,
"multiple stability levels"),
AttrError::UnsupportedLiteral => span_err!(diag, span, E0565, "unsupported literal"),
AttrError::UnsupportedLiteral(
msg,
is_bytestr,
) => {
let mut err = struct_span_err!(diag, span, E0565, "{}", msg);
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();
}
}
}
@ -161,12 +178,12 @@ pub fn contains_feature_attr(attrs: &[Attribute], feature_name: &str) -> bool {
}
/// Find the first stability attribute. `None` if none exists.
pub fn find_stability(diagnostic: &Handler, attrs: &[Attribute],
pub fn find_stability(sess: &ParseSess, attrs: &[Attribute],
item_sp: Span) -> Option<Stability> {
find_stability_generic(diagnostic, attrs.iter(), item_sp)
find_stability_generic(sess, attrs.iter(), item_sp)
}
fn find_stability_generic<'a, I>(diagnostic: &Handler,
fn find_stability_generic<'a, I>(sess: &ParseSess,
attrs_iter: I,
item_sp: Span)
-> Option<Stability>
@ -178,6 +195,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
let mut rustc_depr: Option<RustcDeprecation> = None;
let mut rustc_const_unstable: Option<Symbol> = None;
let mut promotable = false;
let diagnostic = &sess.span_diagnostic;
'outer: for attr in attrs_iter {
if ![
@ -202,7 +220,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
let meta = meta.as_ref().unwrap();
let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
if item.is_some() {
handle_errors(diagnostic, meta.span, AttrError::MultipleItem(meta.name()));
handle_errors(sess, meta.span, AttrError::MultipleItem(meta.name()));
return false
}
if let Some(v) = meta.value_str() {
@ -229,14 +247,22 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
_ => {
let expected = &[ $( stringify!($name) ),+ ];
handle_errors(
diagnostic,
sess,
mi.span,
AttrError::UnknownMetaItem(mi.name(), expected));
AttrError::UnknownMetaItem(mi.name(), expected),
);
continue 'outer
}
}
} else {
handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral);
handle_errors(
sess,
meta.span,
AttrError::UnsupportedLiteral(
"unsupported literal",
false,
),
);
continue 'outer
}
}
@ -261,7 +287,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
})
}
(None, _) => {
handle_errors(diagnostic, attr.span(), AttrError::MissingSince);
handle_errors(sess, attr.span(), AttrError::MissingSince);
continue
}
_ => {
@ -287,7 +313,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
}
"unstable" => {
if stab.is_some() {
handle_errors(diagnostic, attr.span(), AttrError::MultipleStabilityLevels);
handle_errors(sess, attr.span(), AttrError::MultipleStabilityLevels);
break
}
@ -302,7 +328,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
"issue" => if !get(mi, &mut issue) { continue 'outer },
_ => {
handle_errors(
diagnostic,
sess,
meta.span,
AttrError::UnknownMetaItem(
mi.name(),
@ -313,7 +339,14 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
}
}
} else {
handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral);
handle_errors(
sess,
meta.span,
AttrError::UnsupportedLiteral(
"unsupported literal",
false,
),
);
continue 'outer
}
}
@ -340,7 +373,7 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
})
}
(None, _, _) => {
handle_errors(diagnostic, attr.span(), AttrError::MissingFeature);
handle_errors(sess, attr.span(), AttrError::MissingFeature);
continue
}
_ => {
@ -351,29 +384,41 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
}
"stable" => {
if stab.is_some() {
handle_errors(diagnostic, attr.span(), AttrError::MultipleStabilityLevels);
handle_errors(sess, attr.span(), AttrError::MultipleStabilityLevels);
break
}
let mut feature = None;
let mut since = None;
for meta in metas {
if let NestedMetaItemKind::MetaItem(ref mi) = meta.node {
match &*mi.name().as_str() {
"feature" => if !get(mi, &mut feature) { continue 'outer },
"since" => if !get(mi, &mut since) { continue 'outer },
_ => {
handle_errors(
diagnostic,
meta.span,
AttrError::UnknownMetaItem(mi.name(), &["since", "note"]),
);
continue 'outer
match &meta.node {
NestedMetaItemKind::MetaItem(mi) => {
match &*mi.name().as_str() {
"feature" => if !get(mi, &mut feature) { continue 'outer },
"since" => if !get(mi, &mut since) { continue 'outer },
_ => {
handle_errors(
sess,
meta.span,
AttrError::UnknownMetaItem(
mi.name(), &["since", "note"],
),
);
continue 'outer
}
}
},
NestedMetaItemKind::Literal(lit) => {
handle_errors(
sess,
lit.span,
AttrError::UnsupportedLiteral(
"unsupported literal",
false,
),
);
continue 'outer
}
} else {
handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral);
continue 'outer
}
}
@ -390,11 +435,11 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
})
}
(None, _) => {
handle_errors(diagnostic, attr.span(), AttrError::MissingFeature);
handle_errors(sess, attr.span(), AttrError::MissingFeature);
continue
}
_ => {
handle_errors(diagnostic, attr.span(), AttrError::MissingSince);
handle_errors(sess, attr.span(), AttrError::MissingSince);
continue
}
}
@ -462,7 +507,15 @@ pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Feat
error(cfg.span, "unexpected parentheses after `cfg` predicate key")
}
MetaItemKind::NameValue(lit) if !lit.node.is_str() => {
error(lit.span, "literal in `cfg` predicate value must be a string")
handle_errors(
sess,
lit.span,
AttrError::UnsupportedLiteral(
"literal in `cfg` predicate value must be a string",
lit.node.is_bytestr()
),
);
true
}
MetaItemKind::NameValue(..) | MetaItemKind::Word => {
sess.config.contains(&(cfg.name(), cfg.value_str()))
@ -481,7 +534,14 @@ pub fn eval_condition<F>(cfg: &ast::MetaItem, sess: &ParseSess, eval: &mut F)
ast::MetaItemKind::List(ref mis) => {
for mi in mis.iter() {
if !mi.is_meta_item() {
handle_errors(&sess.span_diagnostic, mi.span, AttrError::UnsupportedLiteral);
handle_errors(
sess,
mi.span,
AttrError::UnsupportedLiteral(
"unsupported literal",
false
),
);
return false;
}
}
@ -523,18 +583,19 @@ pub struct Deprecation {
}
/// Find the deprecation attribute. `None` if none exists.
pub fn find_deprecation(diagnostic: &Handler, attrs: &[Attribute],
pub fn find_deprecation(sess: &ParseSess, attrs: &[Attribute],
item_sp: Span) -> Option<Deprecation> {
find_deprecation_generic(diagnostic, attrs.iter(), item_sp)
find_deprecation_generic(sess, attrs.iter(), item_sp)
}
fn find_deprecation_generic<'a, I>(diagnostic: &Handler,
fn find_deprecation_generic<'a, I>(sess: &ParseSess,
attrs_iter: I,
item_sp: Span)
-> Option<Deprecation>
where I: Iterator<Item = &'a Attribute>
{
let mut depr: Option<Deprecation> = None;
let diagnostic = &sess.span_diagnostic;
'outer: for attr in attrs_iter {
if attr.path != "deprecated" {
@ -551,14 +612,27 @@ fn find_deprecation_generic<'a, I>(diagnostic: &Handler,
depr = if let Some(metas) = attr.meta_item_list() {
let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
if item.is_some() {
handle_errors(diagnostic, meta.span, AttrError::MultipleItem(meta.name()));
handle_errors(sess, meta.span, AttrError::MultipleItem(meta.name()));
return false
}
if let Some(v) = meta.value_str() {
*item = Some(v);
true
} else {
span_err!(diagnostic, meta.span, E0551, "incorrect meta item");
if let Some(lit) = meta.name_value_literal() {
handle_errors(
sess,
lit.span,
AttrError::UnsupportedLiteral(
"literal in `deprecated` \
value must be a string",
lit.node.is_bytestr()
),
);
} else {
span_err!(diagnostic, meta.span, E0551, "incorrect meta item");
}
false
}
};
@ -566,22 +640,32 @@ fn find_deprecation_generic<'a, I>(diagnostic: &Handler,
let mut since = None;
let mut note = None;
for meta in metas {
if let NestedMetaItemKind::MetaItem(ref mi) = meta.node {
match &*mi.name().as_str() {
"since" => if !get(mi, &mut since) { continue 'outer },
"note" => if !get(mi, &mut note) { continue 'outer },
_ => {
handle_errors(
diagnostic,
meta.span,
AttrError::UnknownMetaItem(mi.name(), &["since", "note"]),
);
continue 'outer
match &meta.node {
NestedMetaItemKind::MetaItem(mi) => {
match &*mi.name().as_str() {
"since" => if !get(mi, &mut since) { continue 'outer },
"note" => if !get(mi, &mut note) { continue 'outer },
_ => {
handle_errors(
sess,
meta.span,
AttrError::UnknownMetaItem(mi.name(), &["since", "note"]),
);
continue 'outer
}
}
}
} else {
handle_errors(diagnostic, meta.span, AttrError::UnsupportedLiteral);
continue 'outer
NestedMetaItemKind::Literal(lit) => {
handle_errors(
sess,
lit.span,
AttrError::UnsupportedLiteral(
"item in `deprecated` must be a key/value pair",
false,
),
);
continue 'outer
}
}
}
@ -629,16 +713,24 @@ impl IntType {
/// the same discriminant size that the corresponding C enum would or C
/// structure layout, `packed` to remove padding, and `transparent` to elegate representation
/// concerns to the only non-ZST field.
pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec<ReprAttr> {
pub fn find_repr_attrs(sess: &ParseSess, attr: &Attribute) -> Vec<ReprAttr> {
use self::ReprAttr::*;
let mut acc = Vec::new();
let diagnostic = &sess.span_diagnostic;
if attr.path == "repr" {
if let Some(items) = attr.meta_item_list() {
mark_used(attr);
for item in items {
if !item.is_meta_item() {
handle_errors(diagnostic, item.span, AttrError::UnsupportedLiteral);
handle_errors(
sess,
item.span,
AttrError::UnsupportedLiteral(
"meta item in `repr` must be an identifier",
false,
),
);
continue
}

View file

@ -219,6 +219,15 @@ impl MetaItem {
name_from_path(&self.ident)
}
// #[attribute(name = "value")]
// ^^^^^^^^^^^^^^
pub fn name_value_literal(&self) -> Option<&Lit> {
match &self.node {
MetaItemKind::NameValue(v) => Some(v),
_ => None,
}
}
pub fn value_str(&self) -> Option<Symbol> {
match self.node {
MetaItemKind::NameValue(ref v) => {

View file

@ -355,7 +355,7 @@ pub fn compile(sess: &ParseSess, features: &Features, def: &ast::Item, edition:
}
}
let unstable_feature = attr::find_stability(&sess.span_diagnostic,
let unstable_feature = attr::find_stability(&sess,
&def.attrs, def.span).and_then(|stability| {
if let attr::StabilityLevel::Unstable { issue, .. } = stability.level {
Some((stability.feature, issue))

View file

@ -202,8 +202,8 @@ use syntax::source_map::{self, respan};
use syntax::util::move_map::MoveMap;
use syntax::ptr::P;
use syntax::symbol::{Symbol, keywords};
use syntax::parse::ParseSess;
use syntax_pos::{DUMMY_SP, Span};
use errors::Handler;
use self::ty::{LifetimeBounds, Path, Ptr, PtrTy, Self_, Ty};
@ -412,7 +412,7 @@ impl<'a> TraitDef<'a> {
match *item {
Annotatable::Item(ref item) => {
let is_packed = item.attrs.iter().any(|attr| {
for r in attr::find_repr_attrs(&cx.parse_sess.span_diagnostic, attr) {
for r in attr::find_repr_attrs(&cx.parse_sess, attr) {
if let attr::ReprPacked(_) = r {
return true;
}
@ -811,10 +811,10 @@ impl<'a> TraitDef<'a> {
}
}
fn find_repr_type_name(diagnostic: &Handler, type_attrs: &[ast::Attribute]) -> &'static str {
fn find_repr_type_name(sess: &ParseSess, type_attrs: &[ast::Attribute]) -> &'static str {
let mut repr_type_name = "isize";
for a in type_attrs {
for r in &attr::find_repr_attrs(diagnostic, a) {
for r in &attr::find_repr_attrs(sess, a) {
repr_type_name = match *r {
attr::ReprPacked(_) | attr::ReprSimd | attr::ReprAlign(_) | attr::ReprTransparent =>
continue,
@ -1390,7 +1390,7 @@ impl<'a> MethodDef<'a> {
// discriminant_test = __self0_vi == __self1_vi && __self0_vi == __self2_vi && ...
let mut discriminant_test = cx.expr_bool(sp, true);
let target_type_name = find_repr_type_name(&cx.parse_sess.span_diagnostic, type_attrs);
let target_type_name = find_repr_type_name(&cx.parse_sess, type_attrs);
let mut first_ident = None;
for (&ident, self_arg) in vi_idents.iter().zip(&self_args) {

View file

@ -22,11 +22,14 @@ struct S7;
#[cfg(a = 10)] //~ ERROR literal in `cfg` predicate value must be a string
struct S8;
macro_rules! generate_s9 {
#[cfg(a = b"hi")] //~ ERROR literal in `cfg` predicate value must be a string
struct S9;
macro_rules! generate_s10 {
($expr: expr) => {
#[cfg(feature = $expr)] //~ ERROR `cfg` is not a well-formed meta-item
struct S9;
struct S10;
}
}
generate_s9!(concat!("nonexistent"));
generate_s10!(concat!("nonexistent"));

View file

@ -40,21 +40,28 @@ error[E0537]: invalid predicate `a`
LL | #[cfg(a())] //~ ERROR invalid predicate `a`
| ^^^
error: literal in `cfg` predicate value must be a string
error[E0565]: literal in `cfg` predicate value must be a string
--> $DIR/cfg-attr-syntax-validation.rs:22:11
|
LL | #[cfg(a = 10)] //~ ERROR literal in `cfg` predicate value must be a string
| ^^
error[E0565]: literal in `cfg` predicate value must be a string
--> $DIR/cfg-attr-syntax-validation.rs:25:11
|
LL | #[cfg(a = b"hi")] //~ ERROR literal in `cfg` predicate value must be a string
| ^^^^^ help: consider removing the prefix: `"hi"`
error: `cfg` is not a well-formed meta-item
--> $DIR/cfg-attr-syntax-validation.rs:27:9
--> $DIR/cfg-attr-syntax-validation.rs:30:9
|
LL | #[cfg(feature = $expr)] //~ ERROR `cfg` is not a well-formed meta-item
| ^^^^^^^^^^^^^^^^^^^^^^^ help: expected syntax is: `#[cfg(/* predicate */)]`
...
LL | generate_s9!(concat!("nonexistent"));
| ------------------------------------- in this macro invocation
LL | generate_s10!(concat!("nonexistent"));
| -------------------------------------- in this macro invocation
error: aborting due to 9 previous errors
error: aborting due to 10 previous errors
For more information about this error, try `rustc --explain E0537`.
Some errors occurred: E0537, E0565.
For more information about an error, try `rustc --explain E0537`.

View file

@ -1,4 +1,4 @@
error[E0565]: unsupported literal
error[E0565]: item in `deprecated` must be a key/value pair
--> $DIR/E0565-1.rs:12:14
|
LL | #[deprecated("since")] //~ ERROR E0565

View file

@ -0,0 +1,15 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// repr currently doesn't support literals
#[deprecated(since = b"1.29", note = "hi")] //~ ERROR E0565
struct A { }
fn main() { }

View file

@ -0,0 +1,9 @@
error[E0565]: literal in `deprecated` value must be a string
--> $DIR/E0565-2.rs:12:22
|
LL | #[deprecated(since = b"1.29", note = "hi")] //~ ERROR E0565
| ^^^^^^^ help: consider removing the prefix: `"1.29"`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0565`.

View file

@ -1,4 +1,4 @@
error[E0565]: unsupported literal
error[E0565]: meta item in `repr` must be an identifier
--> $DIR/E0565.rs:12:8
|
LL | #[repr("C")] //~ ERROR E0565