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;
|
2017-05-09 13:23:38 +00:00
|
|
|
use std::sync::Mutex;
|
2016-02-21 19:11:32 +00:00
|
|
|
|
|
|
|
/// Get the configuration file from arguments.
|
2017-08-09 07:30:56 +00:00
|
|
|
pub fn file_from_args(
|
|
|
|
args: &[codemap::Spanned<ast::NestedMetaItemKind>],
|
|
|
|
) -> 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 {
|
2017-09-05 09:33:04 +00:00
|
|
|
ast::MetaItemKind::Word | ast::MetaItemKind::List(_) => {
|
|
|
|
Err(("`conf_file` must be a named value", arg.span))
|
|
|
|
},
|
|
|
|
ast::MetaItemKind::NameValue(ref value) => if let ast::LitKind::Str(ref file, _) = value.node {
|
|
|
|
Ok(Some(file.to_string().into()))
|
|
|
|
} else {
|
|
|
|
Err(("`conf_file` value must be a string", value.span))
|
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),
|
2017-05-09 13:23:38 +00:00
|
|
|
/// Not valid toml or doesn't fit the expected conf format
|
|
|
|
Toml(String),
|
2016-08-23 17:39:36 +00:00
|
|
|
/// Type error.
|
2017-08-09 07:30:56 +00:00
|
|
|
Type(
|
|
|
|
/// The name of the key.
|
|
|
|
&'static str,
|
|
|
|
/// The expected type.
|
|
|
|
&'static str,
|
|
|
|
/// The type we got instead.
|
2017-09-05 09:33:04 +00:00
|
|
|
&'static str,
|
2017-08-09 07:30:56 +00:00
|
|
|
),
|
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),
|
2017-05-09 13:23:38 +00:00
|
|
|
Error::Toml(ref err) => err.fmt(f),
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 13:23:38 +00:00
|
|
|
lazy_static! {
|
|
|
|
static ref ERRORS: Mutex<Vec<Error>> = Mutex::new(Vec::new());
|
|
|
|
}
|
2016-02-21 19:11:32 +00:00
|
|
|
|
2017-05-09 13:23:38 +00:00
|
|
|
macro_rules! define_Conf {
|
|
|
|
($(#[$doc: meta] ($rust_name: ident, $rust_name_str: expr, $default: expr => $($ty: tt)+),)+) => {
|
|
|
|
pub use self::helpers::Conf;
|
|
|
|
mod helpers {
|
|
|
|
/// Type used to store lint configuration.
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all="kebab-case")]
|
|
|
|
#[serde(deny_unknown_fields)]
|
|
|
|
pub struct Conf {
|
2017-09-20 21:59:23 +00:00
|
|
|
$(#[$doc] #[serde(default=$rust_name_str)] #[serde(with=$rust_name_str)]
|
|
|
|
pub $rust_name: define_Conf!(TY $($ty)+),)+
|
2017-05-09 13:23:38 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
#[serde(default)]
|
|
|
|
third_party: Option<::toml::Value>,
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
2017-05-09 13:23:38 +00:00
|
|
|
$(
|
|
|
|
mod $rust_name {
|
|
|
|
use serde;
|
|
|
|
use serde::Deserialize;
|
2017-09-20 21:59:23 +00:00
|
|
|
pub fn deserialize<'de, D: serde::Deserializer<'de>>(deserializer: D)
|
|
|
|
-> Result<define_Conf!(TY $($ty)+), D::Error> {
|
2017-05-09 13:23:38 +00:00
|
|
|
type T = define_Conf!(TY $($ty)+);
|
|
|
|
Ok(T::deserialize(deserializer).unwrap_or_else(|e| {
|
2017-09-20 21:59:23 +00:00
|
|
|
::utils::conf::ERRORS.lock().expect("no threading here")
|
|
|
|
.push(::utils::conf::Error::Toml(e.to_string()));
|
2017-05-09 13:23:38 +00:00
|
|
|
super::$rust_name()
|
|
|
|
}))
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 13:23:38 +00:00
|
|
|
fn $rust_name() -> define_Conf!(TY $($ty)+) {
|
|
|
|
define_Conf!(DEFAULT $($ty)+, $default)
|
|
|
|
}
|
|
|
|
)+
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// hack to convert tts
|
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) => {{
|
2017-05-09 13:23:38 +00:00
|
|
|
let slice = $value.as_array();
|
2016-02-21 19:11:32 +00:00
|
|
|
|
|
|
|
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
|
2017-05-09 13:23:38 +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
|
2017-05-09 13:23:38 +00:00
|
|
|
(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
|
2017-05-09 13:23:38 +00:00
|
|
|
(doc_valid_idents, "doc_valid_idents", [
|
2017-05-06 19:31:54 +00:00
|
|
|
"KiB", "MiB", "GiB", "TiB", "PiB", "EiB",
|
2016-12-21 11:30:41 +00:00
|
|
|
"DirectX",
|
2017-03-03 11:28:15 +00:00
|
|
|
"ECMAScript",
|
2016-12-21 11:30:41 +00:00
|
|
|
"GPLv2", "GPLv3",
|
2018-01-02 12:51:35 +00:00
|
|
|
"GitHub", "GitLab",
|
2016-12-21 11:30:41 +00:00
|
|
|
"IPv4", "IPv6",
|
|
|
|
"JavaScript",
|
2018-01-02 12:51:35 +00:00
|
|
|
"NaN", "NaNs",
|
2016-12-21 11:30:41 +00:00
|
|
|
"OAuth",
|
2017-11-18 18:13:07 +00:00
|
|
|
"OpenGL", "OpenSSH", "OpenSSL", "OpenStreetMap",
|
2016-12-21 11:30:41 +00:00
|
|
|
"TrueType",
|
|
|
|
"iOS", "macOS",
|
2017-09-18 20:40:00 +00:00
|
|
|
"TeX", "LaTeX", "BibTeX", "BibLaTeX",
|
2017-04-25 09:38:37 +00:00
|
|
|
"MinGW",
|
2016-12-21 11:30:41 +00:00
|
|
|
] => 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
|
2017-05-09 13:23:38 +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
|
2017-05-09 13:23:38 +00:00
|
|
|
(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
|
2017-05-09 13:23:38 +00:00
|
|
|
(single_char_binding_names_threshold, "single_char_binding_names_threshold", 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
|
2017-05-09 13:23:38 +00:00
|
|
|
(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
|
2017-05-09 13:23:38 +00:00
|
|
|
(enum_variant_name_threshold, "enum_variant_name_threshold", 3 => u64),
|
2017-01-30 12:17:56 +00:00
|
|
|
/// Lint: LARGE_ENUM_VARIANT. The maximum size of a emum's variant to avoid box suggestion
|
2017-05-09 13:23:38 +00:00
|
|
|
(enum_variant_size_threshold, "enum_variant_size_threshold", 200 => u64),
|
2017-09-26 15:54:08 +00:00
|
|
|
/// Lint: VERBOSE_BIT_MASK. The maximum allowed size of a bit mask before suggesting to use 'trailing_zeros'
|
2017-09-25 20:38:49 +00:00
|
|
|
(verbose_bit_mask_threshold, "verbose_bit_mask_threshold", 1 => 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.
|
2017-10-20 12:41:24 +00:00
|
|
|
const CONFIG_FILE_NAMES: [&str; 2] = [".clippy.toml", "clippy.toml"];
|
2016-10-25 17:41:24 +00:00
|
|
|
|
|
|
|
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.
|
2017-09-05 09:33:04 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 14:02:48 +00:00
|
|
|
/// Produces a `Conf` filled with the default values and forwards the errors
|
|
|
|
///
|
|
|
|
/// Used internally for convenience
|
2017-05-09 13:23:38 +00:00
|
|
|
fn default(errors: Vec<Error>) -> (Conf, Vec<Error>) {
|
|
|
|
(toml::from_str("").expect("we never error on empty config files"), errors)
|
|
|
|
}
|
|
|
|
|
2016-10-25 17:41:24 +00:00
|
|
|
/// 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>) {
|
|
|
|
let path = if let Some(path) = path {
|
|
|
|
path
|
|
|
|
} else {
|
2017-05-12 10:09:52 +00:00
|
|
|
return default(Vec::new());
|
2016-10-25 17:41:24 +00:00
|
|
|
};
|
|
|
|
|
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) {
|
2017-05-12 10:09:52 +00:00
|
|
|
return default(vec![err.into()]);
|
2016-03-06 14:48:56 +00:00
|
|
|
}
|
|
|
|
|
2016-02-21 19:11:32 +00:00
|
|
|
buf
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2017-05-09 13:23:38 +00:00
|
|
|
Err(err) => return default(vec![err.into()]),
|
2016-02-21 19:11:32 +00:00
|
|
|
};
|
|
|
|
|
2017-08-09 07:30:56 +00:00
|
|
|
assert!(
|
|
|
|
ERRORS
|
|
|
|
.lock()
|
|
|
|
.expect("no threading -> mutex always safe")
|
|
|
|
.is_empty()
|
|
|
|
);
|
2017-05-09 13:23:38 +00:00
|
|
|
match toml::from_str(&file) {
|
2017-08-09 07:30:56 +00:00
|
|
|
Ok(toml) => (
|
|
|
|
toml,
|
|
|
|
ERRORS
|
|
|
|
.lock()
|
|
|
|
.expect("no threading -> mutex always safe")
|
|
|
|
.split_off(0),
|
|
|
|
),
|
2017-05-09 13:23:38 +00:00
|
|
|
Err(e) => {
|
2017-08-09 07:30:56 +00:00
|
|
|
let mut errors = ERRORS
|
|
|
|
.lock()
|
|
|
|
.expect("no threading -> mutex always safe")
|
|
|
|
.split_off(0);
|
2017-05-09 13:23:38 +00:00
|
|
|
errors.push(Error::Toml(e.to_string()));
|
|
|
|
default(errors)
|
|
|
|
},
|
2016-02-21 19:11:32 +00:00
|
|
|
}
|
|
|
|
}
|