From 4ed5249c523253e55f5c3842fee0d130aeb5877c Mon Sep 17 00:00:00 2001 From: Mateusz Kowalski Date: Wed, 8 Jan 2025 15:12:32 +0100 Subject: [PATCH] Remove cairo linter cli --- Cargo.lock | 187 ------------------ Cargo.toml | 4 - crates/cairo-lint-cli/Cargo.toml | 30 --- crates/cairo-lint-cli/src/helpers.rs | 179 ----------------- crates/cairo-lint-cli/src/main.rs | 280 --------------------------- 5 files changed, 680 deletions(-) delete mode 100644 crates/cairo-lint-cli/Cargo.toml delete mode 100644 crates/cairo-lint-cli/src/helpers.rs delete mode 100644 crates/cairo-lint-cli/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index eeb7197..63cbe41 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -173,12 +173,6 @@ dependencies = [ "serde", ] -[[package]] -name = "bumpalo" -version = "3.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" - [[package]] name = "byte-slice-cast" version = "1.2.2" @@ -703,15 +697,6 @@ dependencies = [ "xshell", ] -[[package]] -name = "camino" -version = "1.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b96ec4966b5813e2c0507c1f86115c8c5abaadc3980879c3424042a02fd1ad3" -dependencies = [ - "serde", -] - [[package]] name = "cfg-if" version = "1.0.0" @@ -774,19 +759,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "console" -version = "0.15.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea3c6ecd8059b57859df5c69830340ed3c41d30e3da0c1cbed90a96ac853041b" -dependencies = [ - "encode_unicode", - "libc", - "once_cell", - "unicode-width", - "windows-sys", -] - [[package]] name = "const-fnv1a-hash" version = "1.1.0" @@ -960,12 +932,6 @@ dependencies = [ "log", ] -[[package]] -name = "encode_unicode" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0" - [[package]] name = "equivalent" version = "1.0.1" @@ -1158,19 +1124,6 @@ dependencies = [ "serde", ] -[[package]] -name = "indicatif" -version = "0.17.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cbf675b85ed934d3c67b5c5469701eec7db22689d0a2139d856e0925fa28b281" -dependencies = [ - "console", - "number_prefix", - "portable-atomic", - "unicode-width", - "web-time", -] - [[package]] name = "indoc" version = "2.0.5" @@ -1216,16 +1169,6 @@ version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" -[[package]] -name = "js-sys" -version = "0.3.76" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6717b6b5b077764fb5966237269cb3c64edddde4b14ce42647430a78ced9e7b7" -dependencies = [ - "once_cell", - "wasm-bindgen", -] - [[package]] name = "keccak" version = "0.1.5" @@ -1421,12 +1364,6 @@ dependencies = [ "autocfg", ] -[[package]] -name = "number_prefix" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" - [[package]] name = "once_cell" version = "1.20.2" @@ -1738,59 +1675,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "scarb-cairo-lint" -version = "0.1.0" -dependencies = [ - "annotate-snippets", - "anyhow", - "cairo-lang-compiler", - "cairo-lang-defs", - "cairo-lang-diagnostics", - "cairo-lang-filesystem", - "cairo-lang-semantic", - "cairo-lang-starknet", - "cairo-lang-syntax", - "cairo-lang-test-plugin", - "cairo-lang-utils", - "cairo-lint-core", - "clap", - "scarb-metadata", - "scarb-ui", - "semver", - "smol_str", -] - -[[package]] -name = "scarb-metadata" -version = "1.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a8b71f63999dbb6d269fbc6fd61310016ab3a160fb13e52a6511a2b904359f0" -dependencies = [ - "camino", - "semver", - "serde", - "serde_json", - "thiserror", -] - -[[package]] -name = "scarb-ui" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19e63037ffa17459b3c07e385498d48fbf9b329e43a93f6c4a751845441fee33" -dependencies = [ - "anyhow", - "camino", - "clap", - "console", - "indicatif", - "scarb-metadata", - "serde", - "serde_json", - "tracing-core", -] - [[package]] name = "schemars" version = "0.8.21" @@ -2141,7 +2025,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" dependencies = [ "once_cell", - "valuable", ] [[package]] @@ -2199,12 +2082,6 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" -[[package]] -name = "valuable" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" - [[package]] name = "version_check" version = "0.9.5" @@ -2227,70 +2104,6 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" -[[package]] -name = "wasm-bindgen" -version = "0.2.99" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a474f6281d1d70c17ae7aa6a613c87fce69a127e2624002df63dcb39d6cf6396" -dependencies = [ - "cfg-if", - "once_cell", - "wasm-bindgen-macro", -] - -[[package]] -name = "wasm-bindgen-backend" -version = "0.2.99" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f89bb38646b4f81674e8f5c3fb81b562be1fd936d84320f3264486418519c79" -dependencies = [ - "bumpalo", - "log", - "proc-macro2", - "quote", - "syn 2.0.90", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-macro" -version = "0.2.99" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2cc6181fd9a7492eef6fef1f33961e3695e4579b9872a6f7c83aee556666d4fe" -dependencies = [ - "quote", - "wasm-bindgen-macro-support", -] - -[[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.99" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30d7a95b763d3c45903ed6c81f156801839e5ee968bb07e534c44df0fcd330c2" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.90", - "wasm-bindgen-backend", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-shared" -version = "0.2.99" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "943aab3fdaaa029a6e0271b35ea10b72b943135afe9bffca82384098ad0e06a6" - -[[package]] -name = "web-time" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" -dependencies = [ - "js-sys", - "wasm-bindgen", -] - [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index 17b2b8b..835cada 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,6 @@ [workspace] resolver = "2" members = [ - "crates/cairo-lint-cli", "crates/cairo-lint-core", "crates/cairo-lint-test-utils", "xtask" @@ -37,7 +36,6 @@ cairo-lang-diagnostics = "*" cairo-lang-test-plugin = "*" cairo-lang-syntax = "*" cairo-lang-defs = "*" -cairo-lang-starknet = "*" cairo-lang-test-utils = "*" test-case = "3.0" pretty_assertions = "1.4.1" @@ -47,7 +45,6 @@ itertools = "0.13.0" log = "0.4.22" clap = { version = "4.5.23", features = ["derive"]} anyhow = "1.0.95" -smol_str = "0.2.2" annotate-snippets = "0.11.5" num-bigint = "0.4.6" if_chain = "1.0.2" @@ -66,5 +63,4 @@ cairo-lang-diagnostics = { git = "https://github.com/starkware-libs/cairo", rev cairo-lang-test-plugin = { git = "https://github.com/starkware-libs/cairo", rev = "3848dce8591446f9bbcc036fef391927d13dccc6" } cairo-lang-syntax = { git = "https://github.com/starkware-libs/cairo", rev = "3848dce8591446f9bbcc036fef391927d13dccc6" } cairo-lang-defs = { git = "https://github.com/starkware-libs/cairo", rev = "3848dce8591446f9bbcc036fef391927d13dccc6" } -cairo-lang-starknet = { git = "https://github.com/starkware-libs/cairo", rev = "3848dce8591446f9bbcc036fef391927d13dccc6" } cairo-lang-test-utils = { git = "https://github.com/starkware-libs/cairo", rev = "3848dce8591446f9bbcc036fef391927d13dccc6" } diff --git a/crates/cairo-lint-cli/Cargo.toml b/crates/cairo-lint-cli/Cargo.toml deleted file mode 100644 index 477d9aa..0000000 --- a/crates/cairo-lint-cli/Cargo.toml +++ /dev/null @@ -1,30 +0,0 @@ -[package] -name = "scarb-cairo-lint" -version.workspace = true -edition.workspace = true -repository.workspace = true -license-file.workspace = true - -[[bin]] -name = "scarb-cairo-lint" -path = "src/main.rs" - -[dependencies] -cairo-lang-compiler.workspace = true -cairo-lang-utils.workspace = true -cairo-lang-semantic.workspace = true -cairo-lang-filesystem.workspace = true -cairo-lang-diagnostics.workspace = true -cairo-lang-syntax.workspace = true -cairo-lang-test-plugin.workspace = true -cairo-lang-defs.workspace = true -cairo-lang-starknet.workspace = true -clap = { workspace = true, features = ["derive"] } -scarb-ui = "0.1.5" -anyhow.workspace = true -scarb-metadata = "1.13.0" -cairo-lint-core = { path = "../cairo-lint-core" } -semver = "1.0.24" -# scarb = { git = "https://github.com/software-mansion/scarb", branch = "main" } -smol_str.workspace = true -annotate-snippets.workspace = true diff --git a/crates/cairo-lint-cli/src/helpers.rs b/crates/cairo-lint-cli/src/helpers.rs deleted file mode 100644 index 122aa3e..0000000 --- a/crates/cairo-lint-cli/src/helpers.rs +++ /dev/null @@ -1,179 +0,0 @@ -use std::collections::BTreeMap; -use std::path::PathBuf; - -use anyhow::{anyhow, Result}; -use cairo_lang_compiler::project::{AllCratesConfig, ProjectConfig, ProjectConfigContent}; -use cairo_lang_filesystem::cfg::{Cfg as CompilerCfg, CfgSet}; -use cairo_lang_filesystem::db::{ - CrateIdentifier, CrateSettings, DependencySettings, Edition, ExperimentalFeaturesConfig, -}; -use cairo_lang_utils::ordered_hash_map::OrderedHashMap; -use scarb_metadata::{Cfg as ScarbCfg, CompilationUnitMetadata, PackageId, PackageMetadata}; -use semver::Version; -use smol_str::ToSmolStr; - -/// Different targets for cairo. -pub mod targets { - /// [lib] - pub const LIB: &str = "lib"; - /// #[cfg(test)] - pub const TEST: &str = "test"; - /// Starknet smart contracts - pub const STARKNET_CONTRACT: &str = "starknet-contract"; - /// All the targets - pub const TARGETS: [&str; 3] = [LIB, TEST, STARKNET_CONTRACT]; -} - -/// Converts [`&[ScarbCfg]`] to a [`CfgSet`] -pub fn to_cairo_cfg(cfgs: &[ScarbCfg]) -> CfgSet { - let mut cfg_set = CfgSet::new(); - cfgs.iter().for_each(|cfg| match cfg { - ScarbCfg::KV(key, value) => { - cfg_set.insert(CompilerCfg { - key: key.to_smolstr(), - value: Some(value.to_smolstr()), - }); - } - ScarbCfg::Name(name) => { - cfg_set.insert(CompilerCfg { - key: name.to_smolstr(), - value: None, - }); - } - }); - cfg_set -} - -/// Convert a string to a compiler [`Edition`]. If the edition is unknown it'll return an error. -pub fn to_cairo_edition(edition: &str) -> Result { - match edition { - "2023_01" => Ok(Edition::V2023_01), - "2023_10" => Ok(Edition::V2023_10), - "2023_11" => Ok(Edition::V2023_11), - "2024_07" => Ok(Edition::V2024_07), - _ => Err(anyhow!("Unknown edition {}", edition)), - } -} - -/// Gets a bunch of informations related to the project from several objects. -/// -/// Mostly a copy pasta of -/// https://github.com/software-mansion/scarb/blob/fb34a0ce85e0a46e15f58abd3fbaaf1d3c4bf012/scarb/src/compiler/helpers.rs#L17-L62 -/// but with metadata objects -pub fn build_project_config( - compilation_unit: &CompilationUnitMetadata, - corelib_id: &PackageId, - package_path: PathBuf, - edition: Edition, - version: &Version, - packages: &[PackageMetadata], -) -> Result { - let crate_roots = compilation_unit - .components - .iter() - .filter(|component| &component.package != corelib_id) - .map(|component| { - ( - CrateIdentifier::from(&component.name), - component.source_root().into(), - ) - }) - .collect(); - let crates_config: OrderedHashMap = compilation_unit - .components - .iter() - .map(|component| { - let package = packages - .iter() - .find(|package| package.id == component.package); - let cfg_set = component.cfg.as_ref().map(|cfgs| to_cairo_cfg(cfgs)); - let (package_ed, dependencies) = if let Some(pack) = package { - let mut dependencies: BTreeMap = pack - .dependencies - .iter() - .filter_map(|dependency| { - compilation_unit - .components - .iter() - .find(|compilation_unit_metadata_component| { - compilation_unit_metadata_component.name == dependency.name - }) - .map(|compilation_unit_metadata_component| { - ( - dependency.name.clone(), - DependencySettings { - discriminator: compilation_unit_metadata_component - .discriminator - .as_ref() - .map(ToSmolStr::to_smolstr), - }, - ) - }) - }) - .collect(); - // Adds itself to dependencies - dependencies.insert( - pack.name.clone(), - DependencySettings { - discriminator: component.discriminator.as_ref().map(ToSmolStr::to_smolstr), - }, - ); - ( - pack.edition - .as_ref() - .map_or_else(|| edition, |ed| to_cairo_edition(ed).unwrap()), - dependencies, - ) - } else { - (edition, BTreeMap::default()) - }; - ( - CrateIdentifier::from(&component.name), - CrateSettings { - name: Some(component.name.to_smolstr()), - edition: package_ed, - cfg_set, - dependencies, - experimental_features: ExperimentalFeaturesConfig { - negative_impls: package - .map(|package| { - package - .experimental_features - .contains(&String::from("negative_impls")) - }) - .unwrap_or(false), - coupons: package - .map(|package| { - package - .experimental_features - .contains(&String::from("coupons")) - }) - .unwrap_or(false), - associated_item_constraints: package - .map(|package| { - package - .experimental_features - .contains(&String::from("associated_item_constraints")) - }) - .unwrap_or(false), - }, - version: Some(version.clone()), - }, - ) - }) - .collect(); - let crates_config = AllCratesConfig { - override_map: crates_config, - ..Default::default() - }; - let content = ProjectConfigContent { - crate_roots, - crates_config, - }; - - let project_config = ProjectConfig { - base_path: package_path, - content, - }; - Ok(project_config) -} diff --git a/crates/cairo-lint-cli/src/main.rs b/crates/cairo-lint-cli/src/main.rs deleted file mode 100644 index 531a433..0000000 --- a/crates/cairo-lint-cli/src/main.rs +++ /dev/null @@ -1,280 +0,0 @@ -pub mod helpers; - -use std::cmp::Reverse; -use std::collections::HashMap; -use std::path::PathBuf; - -use annotate_snippets::Renderer; -use anyhow::{anyhow, Result}; -use cairo_lang_compiler::db::RootDatabase; -use cairo_lang_compiler::project::update_crate_roots_from_project_config; -use cairo_lang_defs::db::DefsGroup; -use cairo_lang_diagnostics::{DiagnosticEntry, Maybe}; -use cairo_lang_filesystem::db::{ - init_dev_corelib, CrateIdentifier, FilesGroup, CORELIB_CRATE_NAME, -}; -use cairo_lang_filesystem::ids::{CrateLongId, FileId}; -use cairo_lang_semantic::db::SemanticGroup; -use cairo_lang_semantic::diagnostic::SemanticDiagnosticKind; -use cairo_lang_starknet::starknet_plugin_suite; -use cairo_lang_syntax::node::SyntaxNode; -use cairo_lang_test_plugin::test_plugin_suite; -use cairo_lang_utils::{Upcast, UpcastMut}; -use cairo_lint_core::diagnostics::format_diagnostic; -use cairo_lint_core::fix::{ - apply_import_fixes, collect_unused_imports, fix_semantic_diagnostic, Fix, ImportFix, -}; -use cairo_lint_core::plugin::{ - cairo_lint_plugin_suite, diagnostic_kind_from_message, CairoLintKind, -}; -use clap::Parser; -use helpers::*; -use scarb_metadata::{MetadataCommand, PackageMetadata, TargetMetadata}; -use scarb_ui::args::{PackagesFilter, VerbositySpec}; -use scarb_ui::components::Status; -use scarb_ui::{OutputFormat, Ui}; -use smol_str::SmolStr; - -#[derive(Parser, Debug)] -struct Args { - /// Name of the package. - #[command(flatten)] - packages_filter: PackagesFilter, - /// Path to the file or project to analyze - path: Option, - /// Logging verbosity. - #[command(flatten)] - pub verbose: VerbositySpec, - /// Comma separated list of target names to compile. - #[arg(long, value_delimiter = ',', env = "SCARB_TARGET_NAMES")] - pub target_names: Vec, - /// Should lint the tests. - #[arg(short, long, default_value_t = false)] - pub test: bool, - /// Should fix the lint when it can. - #[arg(short, long, default_value_t = false)] - pub fix: bool, -} - -fn main() -> Result<()> { - let args: Args = Args::parse(); - let ui = Ui::new(args.verbose.clone().into(), OutputFormat::Text); - if let Err(err) = main_inner(&ui, args) { - ui.anyhow(&err); - std::process::exit(1); - } - Ok(()) -} - -fn main_inner(ui: &Ui, args: Args) -> Result<()> { - // Get the scarb project metadata - let metadata = MetadataCommand::new().inherit_stderr().exec()?; - // Get the corelib package metadata - let corelib = metadata - .packages - .iter() - .find(|package| package.name == CORELIB_CRATE_NAME) - .ok_or(anyhow!("Corelib not found"))?; - // Corelib package id - let corelib_id = &corelib.id; - // Corelib path - let corelib = - Into::::into(corelib.manifest_path.parent().as_ref().unwrap()).join("src"); - // Filter the packages that are requested by the user. The test target is a special case and will - // never be linted unless specified with the `--test` flag - - let matched = args.packages_filter.match_many(&metadata)?; - - // Let's lint everything requested - for package in matched { - // Get the current package metadata - let compilation_units = if args.test { - let tests_targets = find_testable_targets(&package); - metadata - .compilation_units - .iter() - .filter(|compilation_unit| { - compilation_unit.package == package.id - || tests_targets.contains(&&compilation_unit.target) - }) - .collect::>() - } else { - vec![metadata - .compilation_units - .iter() - .find(|compilation_unit| compilation_unit.package == package.id) - .unwrap()] - }; - for compilation_unit in compilation_units { - // Print that we're checking this package. - ui.print(Status::new("Checking", &compilation_unit.target.name)); - // Create our db - let mut db = if args.test { - RootDatabase::builder() - .with_plugin_suite(test_plugin_suite()) - .with_plugin_suite(cairo_lint_plugin_suite()) - .with_plugin_suite(starknet_plugin_suite()) - .with_cfg(to_cairo_cfg(&compilation_unit.cfg)) - .build()? - } else { - RootDatabase::builder() - .with_plugin_suite(cairo_lint_plugin_suite()) - .with_plugin_suite(starknet_plugin_suite()) - .with_cfg(to_cairo_cfg(&compilation_unit.cfg)) - .build()? - }; - // Setup the corelib - init_dev_corelib(db.upcast_mut(), corelib.clone()); - // Convert the package edition to a cairo edition. If not specified or not known it will return an - // error. - let edition = to_cairo_edition( - package - .edition - .as_ref() - .ok_or(anyhow!("No edition found for package {}", package.name))?, - )?; - // Get the package path. - let package_path = package.root.clone().into(); - // Get if we should lint panics or not - let cairo_lint_config = package.tool_metadata("cairo-lint"); - let should_lint_panics = if let Some(config) = cairo_lint_config { - config["nopanic"].as_bool().unwrap_or_default() - } else { - false - }; - // Build the config for this package. - let config = build_project_config( - compilation_unit, - corelib_id, - package_path, - edition, - &package.version, - &metadata.packages, - )?; - - let main_component = compilation_unit - .components - .iter() - .find(|component| component.package == compilation_unit.package) - .expect("main component is guaranteed to exist in compilation unit"); - - update_crate_roots_from_project_config(&mut db, &config); - let crate_id = db.intern_crate(CrateLongId::Real { - name: SmolStr::new(&compilation_unit.target.name), - discriminator: Some(CrateIdentifier::from(&main_component.name).clone().into()), - }); - // Get all the diagnostics - let mut diags = Vec::new(); - - for module_id in &*db.crate_modules(crate_id) { - if let Maybe::Ok(module_diags) = db.module_semantic_diagnostics(*module_id) { - diags.push(module_diags); - } - } - - let renderer = Renderer::styled(); - - let diagnostics = diags - .iter() - .flat_map(|diags| { - let all_diags = diags.get_all(); - all_diags - .iter() - .filter(|diag| { - if let SemanticDiagnosticKind::PluginDiagnostic(diag) = &diag.kind { - (matches!( - diagnostic_kind_from_message(&diag.message), - CairoLintKind::Panic - ) && should_lint_panics) - || !matches!( - diagnostic_kind_from_message(&diag.message), - CairoLintKind::Panic - ) - } else { - true - } - }) - .for_each(|diag| ui.print(format_diagnostic(diag, &db, &renderer))); - all_diags - }) - .collect::>(); - - if args.fix { - // Handling unused imports separately as we need to run pre-analysis on the diagnostics. - // to handle complex cases. - let unused_imports: HashMap> = - collect_unused_imports(&db, &diagnostics); - let mut fixes = HashMap::new(); - unused_imports.keys().for_each(|file_id| { - let file_fixes: Vec = - apply_import_fixes(&db, unused_imports.get(file_id).unwrap()); - fixes.insert(*file_id, file_fixes); - }); - - let diags_without_imports = diagnostics - .iter() - .filter(|diag| !matches!(diag.kind, SemanticDiagnosticKind::UnusedImport(_))) - .collect::>(); - - for diag in diags_without_imports { - if let Some((fix_node, fix)) = fix_semantic_diagnostic(&db, diag) { - let location = diag.location(db.upcast()); - fixes - .entry(location.file_id) - .or_insert_with(Vec::new) - .push(Fix { - span: fix_node.span(db.upcast()), - suggestion: fix, - }); - } - } - for (file_id, mut fixes) in fixes.into_iter() { - ui.print(Status::new("Fixing", &file_id.file_name(db.upcast()))); - fixes.sort_by_key(|fix| Reverse(fix.span.start)); - let mut fixable_diagnostics = Vec::with_capacity(fixes.len()); - if fixes.len() <= 1 { - fixable_diagnostics = fixes; - } else { - // Check if we have nested diagnostics. If so it's a nightmare to fix hence just ignore it - for i in 0..fixes.len() - 1 { - let first = fixes[i].span; - let second = fixes[i + 1].span; - if first.start >= second.end { - fixable_diagnostics.push(fixes[i].clone()); - if i == fixes.len() - 1 { - fixable_diagnostics.push(fixes[i + 1].clone()); - } - } - } - } - // Get all the files that need to be fixed - let mut files: HashMap = HashMap::default(); - files.insert( - file_id, - db.file_content(file_id) - .ok_or(anyhow!("{} not found", file_id.file_name(db.upcast())))? - .to_string(), - ); - // Fix the files - for fix in fixable_diagnostics { - // Can't fail we just set the file value. - files.entry(file_id).and_modify(|file| { - file.replace_range(fix.span.to_str_range(), &fix.suggestion) - }); - } - // Dump them in place - std::fs::write(file_id.full_path(db.upcast()), files.get(&file_id).unwrap())? - } - } - } - } - Ok(()) -} - -fn find_testable_targets(package: &PackageMetadata) -> Vec<&TargetMetadata> { - package - .targets - .iter() - .filter(|target| target.kind == "test") - .collect() -}