Reduce nesting in if blocks by short circuiting

This commit is contained in:
Marcin Puc 2021-09-10 21:52:09 +02:00 committed by Martin Nordholts
parent 9124271eaf
commit 372e42f350
5 changed files with 80 additions and 84 deletions

View File

@ -255,18 +255,18 @@ impl HighlightingAssets {
) -> Result<SyntaxReferenceInSet> {
if let Some(language) = language {
let syntax_set = self.get_syntax_set_by_name(language)?;
syntax_set
return syntax_set
.find_syntax_by_token(language)
.map(|syntax| SyntaxReferenceInSet { syntax, syntax_set })
.ok_or_else(|| Error::UnknownSyntax(language.to_owned()))
} else {
.ok_or_else(|| Error::UnknownSyntax(language.to_owned()));
}
let line_syntax = self.get_first_line_syntax(&mut input.reader)?;
// Get the path of the file:
// If this was set by the metadata, that will take priority.
// If it wasn't, it will use the real file path (if available).
let path_str =
input
let path_str = input
.metadata
.user_provided_name
.as_ref()
@ -307,7 +307,6 @@ impl HighlightingAssets {
line_syntax.ok_or_else(|| Error::UndetectedSyntax("[unknown]".into()))
}
}
}
fn get_extension_syntax(&self, file_name: &OsStr) -> Result<Option<SyntaxReferenceInSet>> {
let mut syntax = self.find_syntax_by_file_name(file_name)?;

View File

@ -100,17 +100,14 @@ pub fn get_languages(config: &Config) -> Result<String> {
// Also skip if the 'extension' contains another real extension, likely
// that is a full match file name like 'CMakeLists.txt' and 'Cargo.lock'
if extension.starts_with('.') || Path::new(extension).extension().is_some() {
true
} else {
return true;
}
let test_file = Path::new("test").with_extension(extension);
let syntax_in_set = assets
.get_syntax_for_file_name(test_file, &config.syntax_mapping)
.unwrap(); // safe since .get_syntaxes() above worked
match syntax_in_set {
Some(syntax_in_set) => syntax_in_set.syntax.name == lang_name,
None => false,
}
}
matches!(syntax_in_set, Some(syntax_in_set) if syntax_in_set.syntax.name == lang_name)
});
}

View File

@ -256,18 +256,18 @@ impl<'a> InputReader<'a> {
}
pub(crate) fn read_line(&mut self, buf: &mut Vec<u8>) -> io::Result<bool> {
if self.first_line.is_empty() {
if !self.first_line.is_empty() {
buf.append(&mut self.first_line);
return Ok(true);
}
let res = self.inner.read_until(b'\n', buf).map(|size| size > 0)?;
if self.content_type == Some(ContentType::UTF_16LE) {
self.inner.read_until(0x00, buf).ok();
let _ = self.inner.read_until(0x00, buf);
}
Ok(res)
} else {
buf.append(&mut self.first_line);
Ok(true)
}
}
}

View File

@ -11,13 +11,13 @@ pub fn retrieve_less_version(less_path: &dyn AsRef<OsStr>) -> Option<usize> {
}
fn parse_less_version(output: &[u8]) -> Option<usize> {
if output.starts_with(b"less ") {
if !output.starts_with(b"less ") {
return None;
}
let version = std::str::from_utf8(&output[5..]).ok()?;
let end = version.find(|c: char| !c.is_ascii_digit())?;
version[..end].parse::<usize>().ok()
} else {
None
}
}
#[test]

View File

@ -226,8 +226,9 @@ impl<'a> InteractivePrinter<'a> {
fn create_fake_panel(&self, text: &str) -> String {
if self.panel_width == 0 {
"".to_string()
} else {
return "".to_string();
}
let text_truncated: String = text.chars().take(self.panel_width - 1).collect();
let text_filled: String = format!(
"{}{}",
@ -240,16 +241,15 @@ impl<'a> InteractivePrinter<'a> {
text_filled
}
}
}
fn preprocess(&self, text: &str, cursor: &mut usize) -> String {
if self.config.tab_width > 0 {
expand_tabs(text, self.config.tab_width, cursor)
} else {
return expand_tabs(text, self.config.tab_width, cursor);
}
*cursor += text.len();
text.to_string()
}
}
}
impl<'a> Printer for InteractivePrinter<'a> {