Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
WIP cargo-clippy command
  • Loading branch information
Manishearth authored and yaahc committed Mar 30, 2019
commit a05afe443f48ae70595afc396c2648cdb3c07fc5
76 changes: 76 additions & 0 deletions src/bin/cargo/commands/clippy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use crate::command_prelude::*;

use cargo::ops;

pub fn cli() -> App {
subcommand("clippy-preview")
// subcommand aliases are handled in aliased_command()
// .alias("c")
.about("Check a local package and all of its dependencies for errors")
.arg_package_spec(
"Package(s) to check",
"Check all packages in the workspace",
"Exclude packages from the check",
)
.arg_jobs()
.arg_targets_all(
"Check only this package's library",
"Check only the specified binary",
"Check all binaries",
"Check only the specified example",
"Check all examples",
"Check only the specified test target",
"Check all tests",
"Check only the specified bench target",
"Check all benches",
"Check all targets",
)
.arg_release("Check artifacts in release mode, with optimizations")
.arg(opt("profile", "Profile to build the selected target for").value_name("PROFILE"))
.arg_features()
.arg_target_triple("Check for the target triple")
.arg_target_dir()
.arg_manifest_path()
.arg_message_format()
.after_help(
"\
If the `--package` argument is given, then SPEC is a package ID specification
which indicates which package should be built. If it is not given, then the
current package is built. For more information on SPEC and its format, see the
`cargo help pkgid` command.

All packages in the workspace are checked if the `--all` flag is supplied. The
`--all` flag is automatically assumed for a virtual manifest.
Note that `--exclude` has to be specified in conjunction with the `--all` flag.

Compilation can be configured via the use of profiles which are configured in
the manifest. The default profile for this command is `dev`, but passing
the `--release` flag will use the `release` profile instead.

The `--profile test` flag can be used to check unit tests with the
`#[cfg(test)]` attribute.
",
)
}

pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
config.set_clippy_override(true);
let ws = args.workspace(config)?;
let test = match args.value_of("profile") {
Some("test") => true,
None => false,
Some(profile) => {
let err = failure::format_err!(
"unknown profile: `{}`, only `test` is \
currently supported",
profile
);
return Err(CliError::new(err, 101));
}
};
let mode = CompileMode::Check { test };
let compile_opts = args.compile_options(config, mode, Some(&ws))?;

ops::compile(&ws, &compile_opts)?;
Ok(())
}
3 changes: 3 additions & 0 deletions src/bin/cargo/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub fn builtin() -> Vec<App> {
build::cli(),
check::cli(),
clean::cli(),
clippy::cli(),
doc::cli(),
fetch::cli(),
fix::cli(),
Expand Down Expand Up @@ -41,6 +42,7 @@ pub fn builtin_exec(cmd: &str) -> Option<fn(&mut Config, &ArgMatches<'_>) -> Cli
"build" => build::exec,
"check" => check::exec,
"clean" => clean::exec,
"clippy-preview" => clippy::exec,
"doc" => doc::exec,
"fetch" => fetch::exec,
"fix" => fix::exec,
Expand Down Expand Up @@ -76,6 +78,7 @@ pub mod bench;
pub mod build;
pub mod check;
pub mod clean;
pub mod clippy;
pub mod doc;
pub mod fetch;
pub mod fix;
Expand Down
18 changes: 17 additions & 1 deletion src/cargo/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ pub struct Config {
env: HashMap<String, String>,
/// Profiles loaded from config.
profiles: LazyCell<ConfigProfiles>,
/// Use `clippy-driver` instead of `rustc`
clippy_override: bool,
}

impl Config {
Expand Down Expand Up @@ -129,6 +131,7 @@ impl Config {
target_dir: None,
env,
profiles: LazyCell::new(),
clippy_override: false,
}
}

Expand Down Expand Up @@ -190,16 +193,26 @@ impl Config {
.map(AsRef::as_ref)
}

/// Sets the clippy override. If this is true, clippy-driver is invoked instead of rustc.
pub fn set_clippy_override(&mut self, val: bool) {
self.clippy_override = val;
}

/// Gets the path to the `rustc` executable.
pub fn rustc(&self, ws: Option<&Workspace<'_>>) -> CargoResult<Rustc> {
let cache_location = ws.map(|ws| {
ws.target_dir()
.join(".rustc_info.json")
.into_path_unlocked()
});
let wrapper = if self.clippy_override {
Some(self.get_tool("clippy-driver")?)
} else {
self.maybe_get_tool("rustc-wrapper")?
};
Rustc::new(
self.get_tool("rustc")?,
self.maybe_get_tool("rustc_wrapper")?,
wrapper,
&self
.home()
.join("bin")
Expand Down Expand Up @@ -743,14 +756,17 @@ impl Config {
} else {
PathBuf::from(tool_path)
};
println!("some tool {}", tool);
return Ok(Some(path));
}

let var = format!("build.{}", tool);
if let Some(tool_path) = self.get_path(&var)? {
println!("some tool {}", tool);
return Ok(Some(tool_path.val));
}

println!("NO TOOL {}", tool);
Ok(None)
}

Expand Down