diff options
author | Melody Horn <melody@boringcactus.com> | 2024-11-10 16:02:48 -0700 |
---|---|---|
committer | Melody Horn <melody@boringcactus.com> | 2024-11-10 16:02:48 -0700 |
commit | 7029372c9fada6316852915765cc0d763be3c63c (patch) | |
tree | d649d79de0b83b84637d85595fd6de2cfa38f17c /src/makefile | |
parent | e9080b515d86b9f39e97d4c8e1a157dfa4ba86f3 (diff) | |
download | makers-7029372c9fada6316852915765cc0d763be3c63c.tar.gz makers-7029372c9fada6316852915765cc0d763be3c63c.zip |
clippy moment
Diffstat (limited to 'src/makefile')
-rw-r--r-- | src/makefile/command_line.rs | 9 | ||||
-rw-r--r-- | src/makefile/functions.rs | 26 | ||||
-rw-r--r-- | src/makefile/inference_rules.rs | 7 | ||||
-rw-r--r-- | src/makefile/input.rs | 29 | ||||
-rw-r--r-- | src/makefile/macro.rs | 10 | ||||
-rw-r--r-- | src/makefile/mod.rs | 18 | ||||
-rw-r--r-- | src/makefile/target.rs | 10 | ||||
-rw-r--r-- | src/makefile/token.rs | 2 |
8 files changed, 54 insertions, 57 deletions
diff --git a/src/makefile/command_line.rs b/src/makefile/command_line.rs index 6a45c18..ae6c9e9 100644 --- a/src/makefile/command_line.rs +++ b/src/makefile/command_line.rs @@ -94,14 +94,13 @@ impl CommandLine { #[cfg(feature = "full")] { let is_just_one_macro_expansion = self.execution_line.tokens().count() == 1 - && self.execution_line.tokens().all(|x| match x { - Token::MacroExpansion { .. } => true, - Token::FunctionCall { .. } => true, - _ => false, + && self.execution_line.tokens().all(|x| { + matches!(x, Token::MacroExpansion { .. } | Token::FunctionCall { .. }) }); // unfortunately, if we had a multiline macro somewhere with non-escaped newlines, now we have to run each of them as separate lines lazy_static! { - static ref UNESCAPED_NEWLINE: Regex = Regex::new(r"([^\\])\n").unwrap(); + static ref UNESCAPED_NEWLINE: Regex = #[allow(clippy::unwrap_used)] + Regex::new(r"([^\\])\n").unwrap(); } if is_just_one_macro_expansion && UNESCAPED_NEWLINE.is_match(&execution_line) { let lines = UNESCAPED_NEWLINE diff --git a/src/makefile/functions.rs b/src/makefile/functions.rs index a78d582..5d5e222 100644 --- a/src/makefile/functions.rs +++ b/src/makefile/functions.rs @@ -10,6 +10,7 @@ use super::r#macro::{Macro, Set as MacroSet}; use super::token::TokenString; use super::ItemSource; +#[allow(clippy::cognitive_complexity)] pub fn expand_call( name: &str, args: &[TokenString], @@ -174,17 +175,14 @@ mod text { let from = macros.expand(from)?; let to = macros.expand(to)?; let text = macros.expand(text)?; - let words = text - .split_whitespace() - .map(|word| { - let pattern_match = r#match(&from, word)?.and_then(|x| x.get(1)); - Ok(if let Some(pm) = pattern_match { - to.replace('%', pm.as_str()) - } else { - word.to_owned() + let words = + text.split_whitespace() + .map(|word| { + let pattern_match = r#match(&from, word)?.and_then(|x| x.get(1)); + Ok(pattern_match + .map_or_else(|| word.to_owned(), |pm| to.replace('%', pm.as_str()))) }) - }) - .collect::<Result<Vec<_>>>()?; + .collect::<Result<Vec<_>>>()?; Ok(words.join(" ")) } @@ -273,7 +271,7 @@ mod text { pub fn firstword(macros: &MacroSet, words: &TokenString) -> Result<String> { let words = macros.expand(words)?; - Ok(words.split_whitespace().nth(0).unwrap_or("").to_owned()) + Ok(words.split_whitespace().next().unwrap_or("").to_owned()) } pub fn lastword(macros: &MacroSet, words: &TokenString) -> Result<String> { @@ -419,11 +417,7 @@ mod conditional { condition.trim_end(); let condition = macros.expand(&condition)?; if condition.is_empty() { - if let Some(if_false) = if_false { - macros.expand(if_false) - } else { - Ok(String::new()) - } + if_false.map_or_else(|| Ok(String::new()), |if_false| macros.expand(if_false)) } else { macros.expand(if_true) } diff --git a/src/makefile/inference_rules.rs b/src/makefile/inference_rules.rs index 368d72b..f8bdd65 100644 --- a/src/makefile/inference_rules.rs +++ b/src/makefile/inference_rules.rs @@ -1,6 +1,6 @@ use std::fmt; -use eyre::{eyre, Result}; +use eyre::{eyre, OptionExt, Result}; use regex::Captures; use super::command_line::CommandLine; @@ -49,7 +49,10 @@ impl InferenceRule { let capture = self .first_match(target_name)? .ok_or_else(|| eyre!("asked non-matching inference rule for prerequisites"))?; - let percent_expansion = capture.get(1).expect("should've matched the %").as_str(); + let percent_expansion = capture + .get(1) + .ok_or_eyre("should've matched the %")? + .as_str(); Ok(self .prerequisites .iter() diff --git a/src/makefile/input.rs b/src/makefile/input.rs index 318493b..ed1140e 100644 --- a/src/makefile/input.rs +++ b/src/makefile/input.rs @@ -96,9 +96,11 @@ fn inference_match<'a>( prerequisites: &[String], ) -> Option<InferenceMatch<'a>> { lazy_static! { - static ref INFERENCE_RULE: Regex = - Regex::new(r"^(?P<s2>(\.[^/.]+)?)(?P<s1>\.[^/.]+)$").unwrap(); - static ref SPECIAL_TARGET: Regex = Regex::new(r"^\.[A-Z]+$").unwrap(); + static ref INFERENCE_RULE: Regex = #[allow(clippy::unwrap_used)] + Regex::new(r"^(?P<s2>(\.[^/.]+)?)(?P<s1>\.[^/.]+)$") + .unwrap(); + static ref SPECIAL_TARGET: Regex = #[allow(clippy::unwrap_used)] + Regex::new(r"^\.[A-Z]+$").unwrap(); } let inference_match = INFERENCE_RULE.captures(targets[0]); @@ -109,6 +111,7 @@ fn inference_match<'a>( && inference_match.is_some() && special_target_match.is_none(); if inference_rule { + #[allow(clippy::unwrap_used)] inference_match.map(|x| InferenceMatch { s1: x.name("s1").unwrap().as_str(), s2: x.name("s2").unwrap().as_str(), @@ -128,7 +131,7 @@ where E: StdError + Send + Sync + 'static, Inner: Iterator<Item = Result<T, E>>, { - fn new(inner: Inner) -> Self { + const fn new(inner: Inner) -> Self { Self(inner, 0) } } @@ -302,7 +305,7 @@ impl<'a, 'parent, R: BufRead> MakefileReader<'a, 'parent, R> { let line_type = LineType::of(&line_tokens); // before we actually test it, see if it's only visible after expanding macros - let (line_tokens, line_type) = if let LineType::Unknown = line_type { + let (line_tokens, line_type) = if matches!(line_type, LineType::Unknown) { let line_tokens = TokenString::text( self.expand_macros(&line_tokens) .wrap_err_with(|| format!("while parsing line {}", line_number))? @@ -365,8 +368,7 @@ impl<'a, 'parent, R: BufRead> MakefileReader<'a, 'parent, R> { } else { let exported = if line_tokens.contains_text("=") { // that's an assignment! - let new_macro = self.read_macro(line_tokens, line_number)?; - new_macro + self.read_macro(line_tokens, line_number)? } else { self.expand_macros(&line_tokens)? }; @@ -401,7 +403,8 @@ impl<'a, 'parent, R: BufRead> MakefileReader<'a, 'parent, R> { fn next_line(&mut self, settings: NextLineSettings) -> Option<(usize, Result<String>)> { lazy_static! { - static ref COMMENT: Regex = Regex::new(r"(^|[^\\])#.*$").unwrap(); + static ref COMMENT: Regex = #[allow(clippy::unwrap_used)] + Regex::new(r"(^|[^\\])#.*$").unwrap(); } let escaped_newline_replacement = settings.escaped_newline_replacement; if let Some((line_number, line)) = self.pending_line.take() { @@ -522,17 +525,13 @@ impl<'a, 'parent, R: BufRead> MakefileReader<'a, 'parent, R> { } fn special_target_has_prereq(&self, target: &str, name: &str, empty_counts: bool) -> bool { - match self - .targets + self.targets .get(target) .or_else(|| self.built_in_targets.get(target)) - { - Some(target) => { + .map_or(false, |target| { (empty_counts && target.prerequisites.is_empty()) || target.prerequisites.iter().any(|e| e == name) - } - None => false, - } + }) } fn read_include(&mut self, mut line_tokens: TokenString, line_number: usize) -> Result<()> { diff --git a/src/makefile/macro.rs b/src/makefile/macro.rs index 25f8627..daf770d 100644 --- a/src/makefile/macro.rs +++ b/src/makefile/macro.rs @@ -191,10 +191,10 @@ impl<'parent, 'lookup> Set<'parent, 'lookup> { sne.extend(one); } (ExportConfig::Only(se), ExportConfig::AllBut(one)) => { - se.extend(other.keys().cloned().filter(|name| !one.contains(name))); + se.extend(other.keys().filter(|name| !one.contains(*name)).cloned()); } (ExportConfig::AllBut(sne), ExportConfig::Only(oe)) => { - sne.extend(other.keys().cloned().filter(|name| !oe.contains(name))); + sne.extend(other.keys().filter(|name| !oe.contains(*name)).cloned()); } } self.data.extend(other); @@ -397,6 +397,12 @@ impl fmt::Display for Set<'_, '_> { } } +impl Default for Set<'_, '_> { + fn default() -> Self { + Self::new() + } +} + fn builtins() -> Vec<(&'static str, TokenString)> { // Fuck it, might as well. macro_rules! handle { diff --git a/src/makefile/mod.rs b/src/makefile/mod.rs index 74f1f4b..62cc416 100644 --- a/src/makefile/mod.rs +++ b/src/makefile/mod.rs @@ -161,13 +161,10 @@ impl<'a> Makefile<'a> { } fn special_target_has_prereq(&self, target: &str, name: &str) -> bool { - match self.targets.get(target) { - Some(target) => { - let target = target.borrow(); - target.prerequisites.is_empty() || target.prerequisites.iter().any(|e| e == name) - } - None => false, - } + self.targets.get(target).map_or(false, |target| { + let target = target.borrow(); + target.prerequisites.is_empty() || target.prerequisites.iter().any(|e| e == name) + }) } fn infer_target( @@ -326,10 +323,9 @@ impl<'a> Makefile<'a> { self.targets.put(new_target); } - Ok(self - .targets + self.targets .get(name) - .ok_or_else(|| eyre!("Target {:?} not found!", name))?) + .ok_or_else(|| eyre!("Target {:?} not found!", name)) } pub fn update_target(&self, name: &str) -> Result<()> { @@ -376,7 +372,7 @@ impl<'a> Makefile<'a> { // chosen for the target. In the .DEFAULT rule, the $< macro // shall evaluate to the current target name. // TODO make that actually be the case (rn exists_but_inferring_anyway might fuck that up) - vec![target.prerequisites.get(0).cloned().unwrap_or_default()] + vec![target.prerequisites.first().cloned().unwrap_or_default()] } else if macro_name.starts_with('*') { // The $* macro shall evaluate to the current target name with // its suffix deleted. (GNUism: the match stem) diff --git a/src/makefile/target.rs b/src/makefile/target.rs index c3431e4..d6fac2c 100644 --- a/src/makefile/target.rs +++ b/src/makefile/target.rs @@ -22,7 +22,7 @@ pub struct Target { } impl Target { - pub fn extend(&mut self, other: Target) { + pub fn extend(&mut self, other: Self) { assert_eq!(&self.name, &other.name); match (self.commands.is_empty(), other.commands.is_empty()) { (false, false) => { @@ -154,9 +154,9 @@ impl StaticTargetSet { } } -impl Into<HashMap<String, Target>> for StaticTargetSet { - fn into(self) -> HashMap<String, Target> { - self.data +impl From<StaticTargetSet> for HashMap<String, Target> { + fn from(value: StaticTargetSet) -> Self { + value.data } } @@ -168,7 +168,7 @@ pub struct DynamicTargetSet { impl DynamicTargetSet { pub fn get(&self, name: &str) -> Option<Rc<RefCell<Target>>> { - self.data.borrow().get(name).map(|x| Rc::clone(x)) + self.data.borrow().get(name).map(Rc::clone) } pub fn put(&self, target: Target) { diff --git a/src/makefile/token.rs b/src/makefile/token.rs index 418baf5..396380b 100644 --- a/src/makefile/token.rs +++ b/src/makefile/token.rs @@ -128,7 +128,7 @@ impl TokenString { } pub fn is_empty(&self) -> bool { - match self.0.get(0) { + match self.0.first() { None => true, Some(Token::Text(t)) if t.is_empty() && self.0.len() == 1 => true, _ => false, |