2016-08-23 17:39:36 +00:00
|
|
|
//! Read configurations files.
|
|
|
|
|
|
|
|
#![deny(missing_docs_in_private_items)]
|
|
|
|
|
2016-10-25 17:41:24 +00:00
|
|
|
use std::{env, fmt, fs, io, path};
|
2016-02-21 19:11:32 +00:00
|
|
|
use std::io::Read;
|
2016-08-28 15:54:32 +00:00
|
|
|
use syntax::{ast, codemap};
|
2016-02-21 19:11:32 +00:00
|
|
|
use toml;
|
|
|
|
|
|
|
|
/// Get the configuration file from arguments.
|
2016-12-20 17:21:30 +00:00
|
|
|
pub fn file_from_args(args: &[codemap::Spanned<ast::NestedMetaItemKind>])
|
2016-12-21 09:25:14 +00:00
|
|
|
-> Result<Option<path::PathBuf>, (&'static str, codemap::Span)> {
|
2016-08-28 15:54:32 +00:00
|
|
|
for arg in args.iter().filter_map(|a| a.meta_item()) {
|
2016-11-23 20:19:03 +00:00
|
|
|
if arg.name() == "conf_file" {
|
|
|
|
return match arg.node {
|
|
|
|
ast::MetaItemKind::Word |
|
2016-12-20 17:21:30 +00:00
|
|
|
ast::MetaItemKind::List(_) => Err(("`conf_file` must be a named value", arg.span)),
|
2016-11-23 20:19:03 +00:00
|
|
|
ast::MetaItemKind::NameValue(ref value) => {
|
|
|
|
if let ast::LitKind::Str(ref file, _) = value.node {
|
2016-10-25 17:41:24 +00:00
|
|
|
Ok(Some(file.to_string().into()))
|
2016-02-21 19:11:32 +00:00
|
|
|
} else {
|
|
|
|
Err(("`conf_file` value must be a string", value.span))
|
2016-11-23 20:19:03 +00:00
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-11-23 20:19:03 +00:00
|
|
|
};
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Error from reading a configuration file.
|
|
|
|
#[derive(Debug)]
|
2016-06-10 14:17:20 +00:00
|
|
|
pub enum Error {
|
2016-08-23 17:39:36 +00:00
|
|
|
/// An I/O error.
|
2016-06-10 14:23:17 +00:00
|
|
|
Io(io::Error),
|
2016-08-23 17:39:36 +00:00
|
|
|
/// The file is not valid TOML.
|
2016-06-10 14:23:17 +00:00
|
|
|
Toml(Vec<toml::ParserError>),
|
2016-08-23 17:39:36 +00:00
|
|
|
/// Type error.
|
2016-12-20 17:21:30 +00:00
|
|
|
Type(/// The name of the key.
|
|
|
|
&'static str,
|
|
|
|
/// The expected type.
|
|
|
|
&'static str,
|
|
|
|
/// The type we got instead.
|
|
|
|
&'static str),
|
2016-08-23 17:39:36 +00:00
|
|
|
/// There is an unknown key is the file.
|
2016-02-21 19:11:32 +00:00
|
|
|
UnknownKey(String),
|
|
|
|
}
|
|
|
|
|
2016-06-10 14:17:20 +00:00
|
|
|
impl fmt::Display for Error {
|
2016-02-21 19:11:32 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
match *self {
|
2016-06-10 14:23:17 +00:00
|
|
|
Error::Io(ref err) => err.fmt(f),
|
|
|
|
Error::Toml(ref errs) => {
|
2016-02-21 19:11:32 +00:00
|
|
|
let mut first = true;
|
|
|
|
for err in errs {
|
|
|
|
if !first {
|
|
|
|
try!(", ".fmt(f));
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try!(err.fmt(f));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-08-01 14:59:14 +00:00
|
|
|
Error::Type(key, expected, got) => {
|
2016-02-21 19:11:32 +00:00
|
|
|
write!(f, "`{}` is expected to be a `{}` but is a `{}`", key, expected, got)
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-06-10 14:17:20 +00:00
|
|
|
Error::UnknownKey(ref key) => write!(f, "unknown key `{}`", key),
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 14:17:20 +00:00
|
|
|
impl From<io::Error> for Error {
|
2016-02-21 19:11:32 +00:00
|
|
|
fn from(e: io::Error) -> Self {
|
2016-06-10 14:23:17 +00:00
|
|
|
Error::Io(e)
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! define_Conf {
|
2016-02-22 13:25:51 +00:00
|
|
|
($(#[$doc: meta] ($toml_name: tt, $rust_name: ident, $default: expr => $($ty: tt)+),)+) => {
|
2016-02-21 19:11:32 +00:00
|
|
|
/// Type used to store lint configuration.
|
|
|
|
pub struct Conf {
|
2016-02-22 13:25:51 +00:00
|
|
|
$(#[$doc] pub $rust_name: define_Conf!(TY $($ty)+),)+
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Conf {
|
|
|
|
fn default() -> Conf {
|
|
|
|
Conf {
|
2016-02-22 13:25:51 +00:00
|
|
|
$($rust_name: define_Conf!(DEFAULT $($ty)+, $default),)+
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Conf {
|
|
|
|
/// Set the property `name` (which must be the `toml` name) to the given value
|
|
|
|
#[allow(cast_sign_loss)]
|
2016-06-10 14:17:20 +00:00
|
|
|
fn set(&mut self, name: String, value: toml::Value) -> Result<(), Error> {
|
2016-02-21 19:11:32 +00:00
|
|
|
match name.as_str() {
|
|
|
|
$(
|
|
|
|
define_Conf!(PAT $toml_name) => {
|
2016-02-22 13:25:51 +00:00
|
|
|
if let Some(value) = define_Conf!(CONV $($ty)+, value) {
|
2016-02-21 19:11:32 +00:00
|
|
|
self.$rust_name = value;
|
|
|
|
}
|
|
|
|
else {
|
2016-06-10 14:23:17 +00:00
|
|
|
return Err(Error::Type(define_Conf!(EXPR $toml_name),
|
|
|
|
stringify!($($ty)+),
|
|
|
|
value.type_str()));
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
)+
|
2016-03-06 13:40:25 +00:00
|
|
|
"third-party" => {
|
|
|
|
// for external tools such as clippy-service
|
|
|
|
return Ok(());
|
|
|
|
}
|
2016-02-21 19:11:32 +00:00
|
|
|
_ => {
|
2016-06-10 14:17:20 +00:00
|
|
|
return Err(Error::UnknownKey(name));
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// hack to convert tts
|
|
|
|
(PAT $pat: pat) => { $pat };
|
|
|
|
(EXPR $e: expr) => { $e };
|
2016-02-22 13:25:51 +00:00
|
|
|
(TY $ty: ty) => { $ty };
|
2016-02-21 19:11:32 +00:00
|
|
|
|
|
|
|
// how to read the value?
|
|
|
|
(CONV i64, $value: expr) => { $value.as_integer() };
|
2016-12-21 11:30:41 +00:00
|
|
|
(CONV u64, $value: expr) => {
|
|
|
|
$value.as_integer()
|
|
|
|
.iter()
|
|
|
|
.filter_map(|&i| if i >= 0 { Some(i as u64) } else { None })
|
|
|
|
.next()
|
|
|
|
};
|
2016-02-21 19:11:32 +00:00
|
|
|
(CONV String, $value: expr) => { $value.as_str().map(Into::into) };
|
2016-02-22 13:25:51 +00:00
|
|
|
(CONV Vec<String>, $value: expr) => {{
|
2016-02-21 19:11:32 +00:00
|
|
|
let slice = $value.as_slice();
|
|
|
|
|
|
|
|
if let Some(slice) = slice {
|
|
|
|
if slice.iter().any(|v| v.as_str().is_none()) {
|
|
|
|
None
|
2016-12-21 11:30:41 +00:00
|
|
|
} else {
|
|
|
|
Some(slice.iter().map(|v| v.as_str().expect("already checked").to_owned()).collect())
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
2016-12-21 11:30:41 +00:00
|
|
|
} else {
|
2016-02-21 19:11:32 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
|
2016-02-22 13:25:51 +00:00
|
|
|
// provide a nicer syntax to declare the default value of `Vec<String>` variables
|
|
|
|
(DEFAULT Vec<String>, $e: expr) => { $e.iter().map(|&e| e.to_owned()).collect() };
|
|
|
|
(DEFAULT $ty: ty, $e: expr) => { $e };
|
|
|
|
}
|
2016-02-21 19:11:32 +00:00
|
|
|
|
|
|
|
define_Conf! {
|
2016-02-22 13:25:51 +00:00
|
|
|
/// Lint: BLACKLISTED_NAME. The list of blacklisted names to lint about
|
2016-11-11 04:57:29 +00:00
|
|
|
("blacklisted-names", blacklisted_names, ["foo", "bar", "baz", "quux"] => Vec<String>),
|
2016-02-22 13:25:51 +00:00
|
|
|
/// Lint: CYCLOMATIC_COMPLEXITY. The maximum cyclomatic complexity a function can have
|
|
|
|
("cyclomatic-complexity-threshold", cyclomatic_complexity_threshold, 25 => u64),
|
2016-04-04 18:18:17 +00:00
|
|
|
/// Lint: DOC_MARKDOWN. The list of words this lint should not consider as identifiers needing ticks
|
2016-12-21 11:30:41 +00:00
|
|
|
("doc-valid-idents", doc_valid_idents, [
|
|
|
|
"MiB", "GiB", "TiB", "PiB", "EiB",
|
|
|
|
"DirectX",
|
|
|
|
"GPLv2", "GPLv3",
|
|
|
|
"GitHub",
|
|
|
|
"IPv4", "IPv6",
|
|
|
|
"JavaScript",
|
|
|
|
"NaN",
|
|
|
|
"OAuth",
|
|
|
|
"OpenGL",
|
|
|
|
"TrueType",
|
|
|
|
"iOS", "macOS",
|
|
|
|
] => Vec<String>),
|
2016-02-22 13:25:51 +00:00
|
|
|
/// Lint: TOO_MANY_ARGUMENTS. The maximum number of argument a function or method can have
|
2016-03-08 23:48:10 +00:00
|
|
|
("too-many-arguments-threshold", too_many_arguments_threshold, 7 => u64),
|
2016-02-22 13:25:51 +00:00
|
|
|
/// Lint: TYPE_COMPLEXITY. The maximum complexity a type can have
|
|
|
|
("type-complexity-threshold", type_complexity_threshold, 250 => u64),
|
2016-03-14 13:56:44 +00:00
|
|
|
/// Lint: MANY_SINGLE_CHAR_NAMES. The maximum number of single char bindings a scope may have
|
|
|
|
("single-char-binding-names-threshold", max_single_char_names, 5 => u64),
|
2016-07-10 13:23:50 +00:00
|
|
|
/// Lint: BOXED_LOCAL. The maximum size of objects (in bytes) that will be linted. Larger objects are ok on the heap
|
|
|
|
("too-large-for-stack", too_large_for_stack, 200 => u64),
|
2016-08-06 18:59:27 +00:00
|
|
|
/// Lint: ENUM_VARIANT_NAMES. The minimum number of enum variants for the lints about variant names to trigger
|
|
|
|
("enum-variant-name-threshold", enum_variant_name_threshold, 3 => u64),
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
|
2016-10-25 17:41:24 +00:00
|
|
|
/// Search for the configuration file.
|
|
|
|
pub fn lookup_conf_file() -> io::Result<Option<path::PathBuf>> {
|
|
|
|
/// Possible filename to search for.
|
|
|
|
const CONFIG_FILE_NAMES: [&'static str; 2] = [".clippy.toml", "clippy.toml"];
|
|
|
|
|
|
|
|
let mut current = try!(env::current_dir());
|
|
|
|
|
|
|
|
loop {
|
|
|
|
for config_file_name in &CONFIG_FILE_NAMES {
|
|
|
|
let config_file = current.join(config_file_name);
|
|
|
|
match fs::metadata(&config_file) {
|
|
|
|
// Only return if it's a file to handle the unlikely situation of a directory named
|
|
|
|
// `clippy.toml`.
|
|
|
|
Ok(ref md) if md.is_file() => return Ok(Some(config_file)),
|
|
|
|
// Return the error if it's something other than `NotFound`; otherwise we didn't
|
|
|
|
// find the project file yet, and continue searching.
|
|
|
|
Err(e) => {
|
|
|
|
if e.kind() != io::ErrorKind::NotFound {
|
|
|
|
return Err(e);
|
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-10-25 17:41:24 +00:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the current directory has no parent, we're done searching.
|
|
|
|
if !current.pop() {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read the `toml` configuration file.
|
|
|
|
///
|
2016-03-06 14:48:56 +00:00
|
|
|
/// In case of error, the function tries to continue as much as possible.
|
2016-10-25 17:41:24 +00:00
|
|
|
pub fn read(path: Option<&path::Path>) -> (Conf, Vec<Error>) {
|
2016-02-21 19:11:32 +00:00
|
|
|
let mut conf = Conf::default();
|
2016-03-06 14:48:56 +00:00
|
|
|
let mut errors = Vec::new();
|
2016-02-21 19:11:32 +00:00
|
|
|
|
2016-10-25 17:41:24 +00:00
|
|
|
let path = if let Some(path) = path {
|
|
|
|
path
|
|
|
|
} else {
|
|
|
|
return (conf, errors);
|
|
|
|
};
|
|
|
|
|
2016-02-21 19:11:32 +00:00
|
|
|
let file = match fs::File::open(path) {
|
|
|
|
Ok(mut file) => {
|
|
|
|
let mut buf = String::new();
|
2016-03-06 14:48:56 +00:00
|
|
|
|
|
|
|
if let Err(err) = file.read_to_string(&mut buf) {
|
|
|
|
errors.push(err.into());
|
|
|
|
return (conf, errors);
|
|
|
|
}
|
|
|
|
|
2016-02-21 19:11:32 +00:00
|
|
|
buf
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-21 19:11:32 +00:00
|
|
|
Err(err) => {
|
2016-03-06 14:48:56 +00:00
|
|
|
errors.push(err.into());
|
|
|
|
return (conf, errors);
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-02-21 19:11:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut parser = toml::Parser::new(&file);
|
|
|
|
let toml = if let Some(toml) = parser.parse() {
|
|
|
|
toml
|
2016-02-29 16:48:20 +00:00
|
|
|
} else {
|
2016-06-10 14:23:17 +00:00
|
|
|
errors.push(Error::Toml(parser.errors));
|
2016-03-06 14:48:56 +00:00
|
|
|
return (conf, errors);
|
2016-02-21 19:11:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (key, value) in toml {
|
2016-03-06 14:48:56 +00:00
|
|
|
if let Err(err) = conf.set(key, value) {
|
|
|
|
errors.push(err);
|
|
|
|
}
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
|
2016-03-06 14:48:56 +00:00
|
|
|
(conf, errors)
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|