configparser: address clippy warnings

Summary:
Clippy complains about 3 things:
 - Using raw pointers in a public function that is not declared as unsafe. This
   happens for C exported ones, this feels like a warning, so I haven't changed
   it.
 - Using .map(...).unwrap_or(<default value constructed>). The recommendation
   is to use .unwrap_or_default().
 - Single match instead of if let, the latter makes code much shorter.

Reviewed By: quark-zju

Differential Revision: D20452751

fbshipit-source-id: 8eeff7581c119c651ca41d8117f1f70f15774833
This commit is contained in:
Xavier Deguillard 2020-03-16 14:50:58 -07:00 committed by Facebook GitHub Bot
parent 1fb5acf242
commit 82d3c7f544
2 changed files with 22 additions and 42 deletions

View File

@ -125,7 +125,7 @@ impl ConfigSet {
self.sections self.sections
.get(section.as_ref()) .get(section.as_ref())
.map(|section| section.items.keys().cloned().collect()) .map(|section| section.items.keys().cloned().collect())
.unwrap_or(Vec::new()) .unwrap_or_default()
} }
/// Get config value for a given config. /// Get config value for a given config.
@ -146,13 +146,8 @@ impl ConfigSet {
pub fn get_sources(&self, section: impl AsRef<str>, name: impl AsRef<str>) -> Vec<ValueSource> { pub fn get_sources(&self, section: impl AsRef<str>, name: impl AsRef<str>) -> Vec<ValueSource> {
self.sections self.sections
.get(section.as_ref()) .get(section.as_ref())
.and_then(|section| { .and_then(|section| section.items.get(name.as_ref()).cloned())
section .unwrap_or_default()
.items
.get(name.as_ref())
.map(|values| values.clone())
})
.unwrap_or(Vec::new())
} }
/// Set a config item directly. `section`, `name` locates the config. `value` is the new value. /// Set a config item directly. `section`, `name` locates the config. `value` is the new value.
@ -187,7 +182,7 @@ impl ConfigSet {
if let Some((section, name, value)) = filtered { if let Some((section, name, value)) = filtered {
self.sections self.sections
.entry(section) .entry(section)
.or_insert_with(|| Default::default()) .or_insert_with(Default::default)
.items .items
.entry(name) .entry(name)
.or_insert_with(|| Vec::with_capacity(1)) .or_insert_with(|| Vec::with_capacity(1))
@ -305,13 +300,10 @@ impl ConfigSet {
let handle_section = |pair: Pair, section: &mut Text| { let handle_section = |pair: Pair, section: &mut Text| {
let pairs = pair.into_inner(); let pairs = pair.into_inner();
for pair in pairs { for pair in pairs {
match pair.as_rule() { if let Rule::section_name = pair.as_rule() {
Rule::section_name => {
*section = extract(&buf, pair.as_span()); *section = extract(&buf, pair.as_span());
return; return;
} }
_ => (),
}
} }
unreachable!(); unreachable!();
}; };
@ -319,8 +311,7 @@ impl ConfigSet {
let mut handle_include = |this: &mut ConfigSet, pair: Pair, errors: &mut Vec<Error>| { let mut handle_include = |this: &mut ConfigSet, pair: Pair, errors: &mut Vec<Error>| {
let pairs = pair.into_inner(); let pairs = pair.into_inner();
for pair in pairs { for pair in pairs {
match pair.as_rule() { if let Rule::line = pair.as_rule() {
Rule::line => {
if !skip_include { if !skip_include {
let include_path = pair.as_str(); let include_path = pair.as_str();
let full_include_path = let full_include_path =
@ -328,8 +319,6 @@ impl ConfigSet {
this.load_file(&full_include_path, opts, visited, errors); this.load_file(&full_include_path, opts, visited, errors);
} }
} }
_ => (),
}
} }
}; };
@ -337,23 +326,14 @@ impl ConfigSet {
let unset_span = pair.as_span(); let unset_span = pair.as_span();
let pairs = pair.into_inner(); let pairs = pair.into_inner();
for pair in pairs { for pair in pairs {
match pair.as_rule() { if let Rule::config_name = pair.as_rule() {
Rule::config_name => {
let name = extract(&buf, pair.as_span()); let name = extract(&buf, pair.as_span());
let location = ValueLocation { let location = ValueLocation {
path: shared_path.clone(), path: shared_path.clone(),
content: buf.clone(), content: buf.clone(),
location: unset_span.start()..unset_span.end(), location: unset_span.start()..unset_span.end(),
}; };
return this.set_internal( return this.set_internal(section.clone(), name, None, location.into(), opts);
section.clone(),
name,
None,
location.into(),
opts,
);
}
_ => (),
} }
} }
unreachable!(); unreachable!();

View File

@ -585,7 +585,7 @@ fn parse_list_internal(value: &str) -> Vec<String> {
Some(last) => { Some(last) => {
if last.is_empty() { if last.is_empty() {
1 1
} else if last.ends_with("\\") { } else if last.ends_with('\\') {
2 2
} else { } else {
3 3