Skip to content

Commit a05afe4

Browse files
Manishearthyaahc
authored andcommitted
WIP cargo-clippy command
1 parent 2fc2db1 commit a05afe4

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

src/bin/cargo/commands/clippy.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use crate::command_prelude::*;
2+
3+
use cargo::ops;
4+
5+
pub fn cli() -> App {
6+
subcommand("clippy-preview")
7+
// subcommand aliases are handled in aliased_command()
8+
// .alias("c")
9+
.about("Check a local package and all of its dependencies for errors")
10+
.arg_package_spec(
11+
"Package(s) to check",
12+
"Check all packages in the workspace",
13+
"Exclude packages from the check",
14+
)
15+
.arg_jobs()
16+
.arg_targets_all(
17+
"Check only this package's library",
18+
"Check only the specified binary",
19+
"Check all binaries",
20+
"Check only the specified example",
21+
"Check all examples",
22+
"Check only the specified test target",
23+
"Check all tests",
24+
"Check only the specified bench target",
25+
"Check all benches",
26+
"Check all targets",
27+
)
28+
.arg_release("Check artifacts in release mode, with optimizations")
29+
.arg(opt("profile", "Profile to build the selected target for").value_name("PROFILE"))
30+
.arg_features()
31+
.arg_target_triple("Check for the target triple")
32+
.arg_target_dir()
33+
.arg_manifest_path()
34+
.arg_message_format()
35+
.after_help(
36+
"\
37+
If the `--package` argument is given, then SPEC is a package ID specification
38+
which indicates which package should be built. If it is not given, then the
39+
current package is built. For more information on SPEC and its format, see the
40+
`cargo help pkgid` command.
41+
42+
All packages in the workspace are checked if the `--all` flag is supplied. The
43+
`--all` flag is automatically assumed for a virtual manifest.
44+
Note that `--exclude` has to be specified in conjunction with the `--all` flag.
45+
46+
Compilation can be configured via the use of profiles which are configured in
47+
the manifest. The default profile for this command is `dev`, but passing
48+
the `--release` flag will use the `release` profile instead.
49+
50+
The `--profile test` flag can be used to check unit tests with the
51+
`#[cfg(test)]` attribute.
52+
",
53+
)
54+
}
55+
56+
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
57+
config.set_clippy_override(true);
58+
let ws = args.workspace(config)?;
59+
let test = match args.value_of("profile") {
60+
Some("test") => true,
61+
None => false,
62+
Some(profile) => {
63+
let err = failure::format_err!(
64+
"unknown profile: `{}`, only `test` is \
65+
currently supported",
66+
profile
67+
);
68+
return Err(CliError::new(err, 101));
69+
}
70+
};
71+
let mode = CompileMode::Check { test };
72+
let compile_opts = args.compile_options(config, mode, Some(&ws))?;
73+
74+
ops::compile(&ws, &compile_opts)?;
75+
Ok(())
76+
}

src/bin/cargo/commands/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub fn builtin() -> Vec<App> {
66
build::cli(),
77
check::cli(),
88
clean::cli(),
9+
clippy::cli(),
910
doc::cli(),
1011
fetch::cli(),
1112
fix::cli(),
@@ -41,6 +42,7 @@ pub fn builtin_exec(cmd: &str) -> Option<fn(&mut Config, &ArgMatches<'_>) -> Cli
4142
"build" => build::exec,
4243
"check" => check::exec,
4344
"clean" => clean::exec,
45+
"clippy-preview" => clippy::exec,
4446
"doc" => doc::exec,
4547
"fetch" => fetch::exec,
4648
"fix" => fix::exec,
@@ -76,6 +78,7 @@ pub mod bench;
7678
pub mod build;
7779
pub mod check;
7880
pub mod clean;
81+
pub mod clippy;
7982
pub mod doc;
8083
pub mod fetch;
8184
pub mod fix;

src/cargo/util/config.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ pub struct Config {
7474
env: HashMap<String, String>,
7575
/// Profiles loaded from config.
7676
profiles: LazyCell<ConfigProfiles>,
77+
/// Use `clippy-driver` instead of `rustc`
78+
clippy_override: bool,
7779
}
7880

7981
impl Config {
@@ -129,6 +131,7 @@ impl Config {
129131
target_dir: None,
130132
env,
131133
profiles: LazyCell::new(),
134+
clippy_override: false,
132135
}
133136
}
134137

@@ -190,16 +193,26 @@ impl Config {
190193
.map(AsRef::as_ref)
191194
}
192195

196+
/// Sets the clippy override. If this is true, clippy-driver is invoked instead of rustc.
197+
pub fn set_clippy_override(&mut self, val: bool) {
198+
self.clippy_override = val;
199+
}
200+
193201
/// Gets the path to the `rustc` executable.
194202
pub fn rustc(&self, ws: Option<&Workspace<'_>>) -> CargoResult<Rustc> {
195203
let cache_location = ws.map(|ws| {
196204
ws.target_dir()
197205
.join(".rustc_info.json")
198206
.into_path_unlocked()
199207
});
208+
let wrapper = if self.clippy_override {
209+
Some(self.get_tool("clippy-driver")?)
210+
} else {
211+
self.maybe_get_tool("rustc-wrapper")?
212+
};
200213
Rustc::new(
201214
self.get_tool("rustc")?,
202-
self.maybe_get_tool("rustc_wrapper")?,
215+
wrapper,
203216
&self
204217
.home()
205218
.join("bin")
@@ -743,14 +756,17 @@ impl Config {
743756
} else {
744757
PathBuf::from(tool_path)
745758
};
759+
println!("some tool {}", tool);
746760
return Ok(Some(path));
747761
}
748762

749763
let var = format!("build.{}", tool);
750764
if let Some(tool_path) = self.get_path(&var)? {
765+
println!("some tool {}", tool);
751766
return Ok(Some(tool_path.val));
752767
}
753768

769+
println!("NO TOOL {}", tool);
754770
Ok(None)
755771
}
756772

0 commit comments

Comments
 (0)