Always generate fixes (#4239)

This commit is contained in:
Micha Reiser 2023-05-10 09:06:14 +02:00 committed by GitHub
parent bfa1c28c00
commit 8969ad5879
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
69 changed files with 311 additions and 379 deletions

View file

@ -9,7 +9,7 @@ use filetime::FileTime;
use log::error;
use path_absolutize::Absolutize;
use ruff::message::Message;
use ruff::settings::{flags, AllSettings, Settings};
use ruff::settings::{AllSettings, Settings};
use ruff_cache::{CacheKey, CacheKeyHasher};
use ruff_diagnostics::{DiagnosticKind, Fix};
use ruff_python_ast::imports::ImportMap;
@ -145,7 +145,6 @@ fn cache_key(
package: Option<&Path>,
metadata: &fs::Metadata,
settings: &Settings,
autofix: flags::Autofix,
) -> u64 {
let mut hasher = CacheKeyHasher::new();
CARGO_PKG_VERSION.cache_key(&mut hasher);
@ -158,7 +157,6 @@ fn cache_key(
#[cfg(unix)]
metadata.permissions().mode().cache_key(&mut hasher);
settings.cache_key(&mut hasher);
autofix.cache_key(&mut hasher);
hasher.finish()
}
@ -204,11 +202,10 @@ pub fn get(
package: Option<&Path>,
metadata: &fs::Metadata,
settings: &AllSettings,
autofix: flags::Autofix,
) -> Option<(Vec<Message>, ImportMap)> {
let encoded = read_sync(
&settings.cli.cache_dir,
cache_key(path, package, metadata, &settings.lib, autofix),
cache_key(path, package, metadata, &settings.lib),
)
.ok()?;
match bincode::deserialize::<CheckResult>(&encoded[..]) {
@ -254,14 +251,13 @@ pub fn set(
package: Option<&Path>,
metadata: &fs::Metadata,
settings: &AllSettings,
autofix: flags::Autofix,
messages: &[Message],
imports: &ImportMap,
) {
let check_result = CheckResultRef { imports, messages };
if let Err(e) = write_sync(
&settings.cli.cache_dir,
cache_key(path, package, metadata, &settings.lib, autofix),
cache_key(path, package, metadata, &settings.lib),
&bincode::serialize(&check_result).unwrap(),
) {
error!("Failed to write to cache: {e:?}");
@ -269,15 +265,9 @@ pub fn set(
}
/// Delete a value from the cache.
pub fn del(
path: &Path,
package: Option<&Path>,
metadata: &fs::Metadata,
settings: &AllSettings,
autofix: flags::Autofix,
) {
pub fn del(path: &Path, package: Option<&Path>, metadata: &fs::Metadata, settings: &AllSettings) {
drop(del_sync(
&settings.cli.cache_dir,
cache_key(path, package, metadata, &settings.lib, autofix),
cache_key(path, package, metadata, &settings.lib),
));
}

View file

@ -119,9 +119,7 @@ pub fn lint_path(
&& matches!(autofix, flags::FixMode::None | flags::FixMode::Generate)
{
let metadata = path.metadata()?;
if let Some((messages, imports)) =
cache::get(path, package, &metadata, settings, autofix.into())
{
if let Some((messages, imports)) = cache::get(path, package, &metadata, settings) {
debug!("Cache hit for: {}", path.display());
return Ok(Diagnostics::new(messages, imports));
}
@ -172,26 +170,12 @@ pub fn lint_path(
(result, fixed)
} else {
// If we fail to autofix, lint the original source code.
let result = lint_only(
&contents,
path,
package,
&settings.lib,
noqa,
autofix.into(),
);
let result = lint_only(&contents, path, package, &settings.lib, noqa);
let fixed = FxHashMap::default();
(result, fixed)
}
} else {
let result = lint_only(
&contents,
path,
package,
&settings.lib,
noqa,
autofix.into(),
);
let result = lint_only(&contents, path, package, &settings.lib, noqa);
let fixed = FxHashMap::default();
(result, fixed)
};
@ -209,20 +193,12 @@ pub fn lint_path(
// Purge the cache.
if let Some(metadata) = metadata {
cache::del(path, package, &metadata, settings, autofix.into());
cache::del(path, package, &metadata, settings);
}
} else {
// Re-populate the cache.
if let Some(metadata) = metadata {
cache::set(
path,
package,
&metadata,
settings,
autofix.into(),
&messages,
&imports,
);
cache::set(path, package, &metadata, settings, &messages, &imports);
}
}
@ -305,7 +281,6 @@ pub fn lint_stdin(
package,
settings,
noqa,
autofix.into(),
);
let fixed = FxHashMap::default();
@ -323,7 +298,6 @@ pub fn lint_stdin(
package,
settings,
noqa,
autofix.into(),
);
let fixed = FxHashMap::default();
(result, fixed)

View file

@ -118,7 +118,7 @@ impl Printer {
let num_fixable = diagnostics
.messages
.iter()
.filter(|message| message.kind.fixable)
.filter(|message| message.fix.is_some())
.count();
if num_fixable > 0 {
writeln!(
@ -236,7 +236,7 @@ impl Printer {
(
message.kind.rule(),
&message.kind.body,
message.kind.fixable,
message.fix.is_some(),
)
})
.sorted()