Skip to content

Commit 95e582a

Browse files
committed
Don’t make conf errors fatal errors
1 parent d118b27 commit 95e582a

File tree

2 files changed

+40
-23
lines changed

2 files changed

+40
-23
lines changed

src/lib.rs

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

src/utils/conf.rs

Lines changed: 18 additions & 7 deletions
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)