Skip to content

Commit 4998645

Browse files
committed
Don’t make conf errors fatal errors
1 parent f8ed7ac commit 4998645

File tree

2 files changed

+40
-23
lines changed

2 files changed

+40
-23
lines changed

src/lib.rs

+22-16
Original file line numberDiff line numberDiff line change
@@ -108,24 +108,30 @@ mod reexport {
108108
#[plugin_registrar]
109109
#[cfg_attr(rustfmt, rustfmt_skip)]
110110
pub fn plugin_registrar(reg: &mut Registry) {
111-
let conferr = match utils::conf::conf_file(reg.args()) {
112-
Ok(Some(file_name)) => {
113-
utils::conf::read_conf(&file_name, true)
114-
}
115-
Ok(None) => {
116-
utils::conf::read_conf("Clippy.toml", false)
111+
let conf = match utils::conf::conf_file(reg.args()) {
112+
Ok(file_name) => {
113+
// if the user specified a file, it must exist, otherwise default to `Clippy.toml` but
114+
// do not require the file to exist
115+
let (ref file_name, must_exist) = if let Some(ref file_name) = file_name {
116+
(&**file_name, true)
117+
} else {
118+
("Clippy.toml", false)
119+
};
120+
121+
let (conf, errors) = utils::conf::read_conf(&file_name, must_exist);
122+
123+
// all conf errors are non-fatal, we just use the default conf in case of error
124+
for error in errors {
125+
reg.sess.struct_err(&format!("error reading Clippy's configuration file: {}", error)).emit();
126+
}
127+
128+
conf
117129
}
118130
Err((err, span)) => {
119-
reg.sess.struct_span_err(span, err).emit();
120-
return;
121-
}
122-
};
123-
124-
let conf = match conferr {
125-
Ok(conf) => conf,
126-
Err(err) => {
127-
reg.sess.struct_err(&format!("error reading Clippy's configuration file: {}", err)).emit();
128-
return;
131+
reg.sess.struct_span_err(span, err)
132+
.span_note(span, "Clippy will use defaulf configuration")
133+
.emit();
134+
utils::conf::Conf::default()
129135
}
130136
};
131137

src/utils/conf.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -157,33 +157,44 @@ define_Conf! {
157157

158158
/// Read the `toml` configuration file. The function will ignore “File not found” errors iif
159159
/// `!must_exist`, in which case, it will return the default configuration.
160-
pub fn read_conf(path: &str, must_exist: bool) -> Result<Conf, ConfError> {
160+
/// In case of error, the function tries to continue as much as possible.
161+
pub fn read_conf(path: &str, must_exist: bool) -> (Conf, Vec<ConfError>) {
161162
let mut conf = Conf::default();
163+
let mut errors = Vec::new();
162164

163165
let file = match fs::File::open(path) {
164166
Ok(mut file) => {
165167
let mut buf = String::new();
166-
try!(file.read_to_string(&mut buf));
168+
169+
if let Err(err) = file.read_to_string(&mut buf) {
170+
errors.push(err.into());
171+
return (conf, errors);
172+
}
173+
167174
buf
168175
}
169176
Err(ref err) if !must_exist && err.kind() == io::ErrorKind::NotFound => {
170-
return Ok(conf);
177+
return (conf, errors);
171178
}
172179
Err(err) => {
173-
return Err(err.into());
180+
errors.push(err.into());
181+
return (conf, errors);
174182
}
175183
};
176184

177185
let mut parser = toml::Parser::new(&file);
178186
let toml = if let Some(toml) = parser.parse() {
179187
toml
180188
} else {
181-
return Err(ConfError::TomlError(parser.errors));
189+
errors.push(ConfError::TomlError(parser.errors));
190+
return (conf, errors);
182191
};
183192

184193
for (key, value) in toml {
185-
try!(conf.set(key, value));
194+
if let Err(err) = conf.set(key, value) {
195+
errors.push(err);
196+
}
186197
}
187198

188-
Ok(conf)
199+
(conf, errors)
189200
}

0 commit comments

Comments
 (0)