Skip to content

Commit 20b7559

Browse files
authored
Merge pull request #1296 from Manishearth/features
Be more helping, less helpful and bring convenience back for the dead
2 parents 8630376 + ec893a1 commit 20b7559

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# Change Log
22
All notable changes to this project will be documented in this file.
33

4+
## 0.0.97 — 2016-10-24
5+
* For convenience, `cargo clippy` defines a `cargo-clippy` feature. This was
6+
previously added for a short time under the name `clippy` but removed for
7+
compatibility.
8+
* `cargo clippy --help` is more helping (and less helpful :smile:)
9+
410
## 0.0.96 — 2016-10-22
511
* Rustup to *rustc 1.14.0-nightly (f09420685 2016-10-20)*
612
* New lint: [`iter_skip_next`]

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ You can add options to `allow`/`warn`/`deny`:
163163

164164
Note: `deny` produces errors instead of warnings.
165165

166+
For convenience, `cargo clippy` automatically defines a `cargo-clippy`
167+
features. This lets you set lints level and compile with or without clippy
168+
transparently:
169+
170+
```rust
171+
#[cfg_attr(feature = "cargo-clippy", allow(needless_lifetimes))]
172+
```
173+
166174
## Link with clippy service
167175

168176
`clippy-service` is a rust web initiative providing `rust-clippy` as a web service.

src/main.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// error-pattern:yummy
22
#![feature(box_syntax)]
33
#![feature(rustc_private)]
4+
#![feature(static_in_const)]
45

56
#![allow(unknown_lints, missing_docs_in_private_items)]
67

@@ -110,6 +111,36 @@ impl<'a> CompilerCalls<'a> for ClippyCompilerCalls {
110111

111112
use std::path::Path;
112113

114+
const CARGO_CLIPPY_HELP: &str = r#"Checks a package to catch common mistakes and improve your Rust code.
115+
116+
Usage:
117+
cargo clippy [options] [--] [<opts>...]
118+
119+
Common options:
120+
-h, --help Print this message
121+
--features Features to compile for the package
122+
123+
Other options are the same as `cargo rustc`.
124+
125+
To allow or deny a lint from the command line you can use `cargo clippy --`
126+
with:
127+
128+
-W --warn OPT Set lint warnings
129+
-A --allow OPT Set lint allowed
130+
-D --deny OPT Set lint denied
131+
-F --forbid OPT Set lint forbidden
132+
133+
The feature `cargo-clippy` is automatically defined for convenience. You can use
134+
it to allow or deny lints from the code, eg.:
135+
136+
#[cfg_attr(feature = "cargo-clippy", allow(needless_lifetimes))]
137+
"#;
138+
139+
#[allow(print_stdout)]
140+
fn show_help() {
141+
println!("{}", CARGO_CLIPPY_HELP);
142+
}
143+
113144
pub fn main() {
114145
use std::env;
115146

@@ -138,9 +169,16 @@ pub fn main() {
138169

139170
if let Some("clippy") = std::env::args().nth(1).as_ref().map(AsRef::as_ref) {
140171
// this arm is executed on the initial call to `cargo clippy`
172+
173+
if std::env::args().any(|a| a == "--help" || a == "-h") {
174+
show_help();
175+
return;
176+
}
177+
141178
let manifest_path_arg = std::env::args().skip(2).find(|val| val.starts_with("--manifest-path="));
142179

143180
let mut metadata = cargo::metadata(manifest_path_arg.as_ref().map(AsRef::as_ref)).expect("could not obtain cargo metadata");
181+
144182
assert_eq!(metadata.version, 1);
145183

146184
let manifest_path = manifest_path_arg.map(|arg| PathBuf::from(Path::new(&arg["--manifest-path=".len()..])));
@@ -181,13 +219,16 @@ pub fn main() {
181219

182220
// this conditional check for the --sysroot flag is there so users can call `cargo-clippy` directly
183221
// without having to pass --sysroot or anything
184-
let args: Vec<String> = if env::args().any(|s| s == "--sysroot") {
222+
let mut args: Vec<String> = if env::args().any(|s| s == "--sysroot") {
185223
env::args().collect()
186224
} else {
187225
env::args().chain(Some("--sysroot".to_owned())).chain(Some(sys_root)).collect()
188226
};
227+
189228
// this check ensures that dependencies are built but not linted and the final crate is
190229
// linted but not built
230+
args.extend_from_slice(&["--cfg".to_owned(), r#"feature="cargo-clippy""#.to_owned()]);
231+
191232
let mut ccc = ClippyCompilerCalls::new(env::args().any(|s| s == "-Zno-trans"));
192233
let (result, _) = rustc_driver::run_compiler(&args, &mut ccc, None, None);
193234

@@ -219,6 +260,8 @@ fn process<P, I>(old_args: I, dep_path: P, sysroot: &str) -> Result<(), i32>
219260
args.push(String::from("--sysroot"));
220261
args.push(sysroot.to_owned());
221262
args.push("-Zno-trans".to_owned());
263+
args.push("--cfg".to_owned());
264+
args.push(r#"feature="cargo-clippy""#.to_owned());
222265

223266
let path = std::env::current_exe().expect("current executable path invalid");
224267
let exit_status = std::process::Command::new("cargo")

0 commit comments

Comments
 (0)