Remove a result wrapper from `linter.rs` (#2503)

This commit is contained in:
Charlie Marsh 2023-02-02 18:47:45 -05:00 committed by GitHub
parent bdcab87d2f
commit fa56fabed9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 574 additions and 899 deletions

View File

@ -68,7 +68,7 @@ pub fn lint_path(
// Lint the file.
let (messages, fixed) = if matches!(autofix, fix::FixMode::Apply | fix::FixMode::Diff) {
let (transformed, fixed, messages) = lint_fix(&contents, path, package, &settings.lib)?;
let (transformed, fixed, messages) = lint_fix(&contents, path, package, &settings.lib);
if fixed > 0 {
if matches!(autofix, fix::FixMode::Apply) {
write(path, transformed)?;
@ -84,7 +84,7 @@ pub fn lint_path(
}
(messages, fixed)
} else {
let messages = lint_only(&contents, path, package, &settings.lib, autofix.into())?;
let messages = lint_only(&contents, path, package, &settings.lib, autofix.into());
let fixed = 0;
(messages, fixed)
};
@ -120,7 +120,7 @@ pub fn lint_stdin(
path.unwrap_or_else(|| Path::new("-")),
package,
settings,
)?;
);
if matches!(autofix, fix::FixMode::Apply) {
// Write the contents to stdout, regardless of whether any errors were fixed.
@ -149,7 +149,7 @@ pub fn lint_stdin(
package,
settings,
autofix.into(),
)?;
);
let fixed = 0;
(messages, fixed)
};

View File

@ -32,9 +32,9 @@ pub(crate) fn ignores_from_path<'a>(
HashableGlobMatcher,
HashableHashSet<Rule>,
)],
) -> Result<FxHashSet<&'a Rule>> {
let (file_path, file_basename) = extract_path_names(path)?;
Ok(pattern_code_pairs
) -> FxHashSet<&'a Rule> {
let (file_path, file_basename) = extract_path_names(path).expect("Unable to parse filename");
pattern_code_pairs
.iter()
.filter_map(|(absolute, basename, codes)| {
if basename.is_match(file_basename) {
@ -58,7 +58,7 @@ pub(crate) fn ignores_from_path<'a>(
None
})
.flatten()
.collect())
.collect()
}
/// Convert any path to an absolute path (based on the current working

View File

@ -61,7 +61,7 @@ pub fn check(path: &Path, contents: &str, autofix: bool) -> Result<Vec<Diagnosti
&settings,
autofix.into(),
flags::Noqa::Enabled,
)?;
);
Ok(diagnostics)
}

View File

@ -199,8 +199,7 @@ pub fn check(contents: &str, options: JsValue) -> Result<JsValue, JsValue> {
&settings,
flags::Autofix::Enabled,
flags::Noqa::Enabled,
)
.map_err(|e| e.to_string())?;
);
let messages: Vec<ExpandedMessage> = diagnostics
.into_iter()

View File

@ -39,7 +39,7 @@ pub fn check_path(
settings: &Settings,
autofix: flags::Autofix,
noqa: flags::Noqa,
) -> Result<Vec<Diagnostic>> {
) -> Vec<Diagnostic> {
// Aggregate all diagnostics.
let mut diagnostics: Vec<Diagnostic> = vec![];
@ -146,8 +146,7 @@ pub fn check_path(
// Ignore diagnostics based on per-file-ignores.
if !diagnostics.is_empty() && !settings.per_file_ignores.is_empty() {
let ignores = fs::ignores_from_path(path, &settings.per_file_ignores)?;
let ignores = fs::ignores_from_path(path, &settings.per_file_ignores);
if !ignores.is_empty() {
diagnostics.retain(|diagnostic| !ignores.contains(&diagnostic.kind.rule()));
}
@ -170,7 +169,7 @@ pub fn check_path(
);
}
Ok(diagnostics)
diagnostics
}
const MAX_ITERATIONS: usize = 100;
@ -209,7 +208,7 @@ pub fn add_noqa_to_path(path: &Path, settings: &Settings) -> Result<usize> {
settings,
flags::Autofix::Disabled,
flags::Noqa::Disabled,
)?;
);
add_noqa(
path,
@ -229,7 +228,7 @@ pub fn lint_only(
package: Option<&Path>,
settings: &Settings,
autofix: flags::Autofix,
) -> Result<Vec<Message>> {
) -> Vec<Message> {
// Tokenize once.
let tokens: Vec<LexResult> = rustpython_helpers::tokenize(contents);
@ -259,11 +258,11 @@ pub fn lint_only(
settings,
autofix,
flags::Noqa::Enabled,
)?;
);
// Convert from diagnostics to messages.
let path_lossy = path.to_string_lossy();
Ok(diagnostics
diagnostics
.into_iter()
.map(|diagnostic| {
let source = if settings.show_source {
@ -273,7 +272,7 @@ pub fn lint_only(
};
Message::from_diagnostic(diagnostic, path_lossy.to_string(), source)
})
.collect())
.collect()
}
/// Generate `Diagnostic`s from source code content, iteratively autofixing
@ -283,7 +282,7 @@ pub fn lint_fix(
path: &Path,
package: Option<&Path>,
settings: &Settings,
) -> Result<(String, usize, Vec<Message>)> {
) -> (String, usize, Vec<Message>) {
let mut contents = contents.to_string();
// Track the number of fixed errors across iterations.
@ -323,7 +322,7 @@ pub fn lint_fix(
settings,
flags::Autofix::Enabled,
flags::Noqa::Enabled,
)?;
);
// Apply autofix.
if let Some((fixed_contents, applied)) = fix_file(&diagnostics, &locator) {
@ -372,6 +371,6 @@ quoting the contents of `{}`, along with the `pyproject.toml` settings and execu
Message::from_diagnostic(diagnostic, path_lossy.to_string(), source)
})
.collect();
return Ok((contents, fixed, messages));
return (contents, fixed, messages);
}
}

View File

@ -19,7 +19,7 @@ mod tests {
use crate::test::test_path;
use crate::{assert_yaml_snapshot, directives, rustpython_helpers, settings};
fn rule_code(contents: &str, expected: &[Rule]) -> Result<()> {
fn rule_code(contents: &str, expected: &[Rule]) {
let contents = dedent(contents);
let settings = settings::Settings::for_rules(&RuleCodePrefix::PD);
let tokens: Vec<LexResult> = rustpython_helpers::tokenize(&contents);
@ -40,13 +40,12 @@ mod tests {
&settings,
flags::Autofix::Enabled,
flags::Noqa::Enabled,
)?;
);
let actual = diagnostics
.iter()
.map(|diagnostic| diagnostic.kind.rule().clone())
.collect::<Vec<_>>();
assert_eq!(actual, expected);
Ok(())
}
#[test_case(r#"
@ -244,9 +243,8 @@ mod tests {
import pandas as pd
df = pd.DataFrame()
"#, &[Rule::DfIsABadVariableName]; "PD901_fail_df_var")]
fn test_pandas_vet(code: &str, expected: &[Rule]) -> Result<()> {
rule_code(code, expected)?;
Ok(())
fn test_pandas_vet(code: &str, expected: &[Rule]) {
rule_code(code, expected);
}
#[test_case(Rule::UseOfInplaceArgument, Path::new("PD002.py"); "PD002")]

File diff suppressed because it is too large Load Diff

View File

@ -43,7 +43,7 @@ pub fn test_path(path: &Path, settings: &Settings) -> Result<Vec<Diagnostic>> {
settings,
flags::Autofix::Enabled,
flags::Noqa::Enabled,
)?;
);
// Detect autofixes that don't converge after multiple iterations.
if diagnostics
@ -74,7 +74,7 @@ pub fn test_path(path: &Path, settings: &Settings) -> Result<Vec<Diagnostic>> {
settings,
flags::Autofix::Enabled,
flags::Noqa::Enabled,
)?;
);
if let Some((fixed_contents, _)) = fix_file(&diagnostics, &locator) {
if iterations < max_iterations {
iterations += 1;