@@ -17,8 +17,9 @@ use rustc_driver::{driver, CompilerCalls, RustcDefaultCalls, Compilation};
17
17
use rustc:: session:: { config, Session } ;
18
18
use rustc:: session:: config:: { Input , ErrorOutputType } ;
19
19
use std:: path:: PathBuf ;
20
- use std:: process:: Command ;
20
+ use std:: process:: { self , Command } ;
21
21
use syntax:: ast;
22
+ use std:: io:: { self , Write } ;
22
23
23
24
use clippy_lints:: utils:: cargo;
24
25
@@ -118,6 +119,7 @@ Usage:
118
119
Common options:
119
120
-h, --help Print this message
120
121
--features Features to compile for the package
122
+ -V, --version Print version info and exit
121
123
122
124
Other options are the same as `cargo rustc`.
123
125
@@ -140,26 +142,41 @@ fn show_help() {
140
142
println ! ( "{}" , CARGO_CLIPPY_HELP ) ;
141
143
}
142
144
145
+ #[ allow( print_stdout) ]
146
+ fn show_version ( ) {
147
+ println ! ( "{}" , env!( "CARGO_PKG_VERSION" ) ) ;
148
+ }
149
+
143
150
pub fn main ( ) {
144
151
use std:: env;
145
152
146
153
if env:: var ( "CLIPPY_DOGFOOD" ) . map ( |_| true ) . unwrap_or ( false ) {
147
154
panic ! ( "yummy" ) ;
148
155
}
156
+
157
+ // Check for version and help flags even when invoked as 'cargo-clippy'
158
+ if std:: env:: args ( ) . any ( |a| a == "--help" || a == "-h" ) {
159
+ show_help ( ) ;
160
+ return ;
161
+ }
162
+ if std:: env:: args ( ) . any ( |a| a == "--version" || a == "-V" ) {
163
+ show_version ( ) ;
164
+ return ;
165
+ }
149
166
150
167
let dep_path = env:: current_dir ( ) . expect ( "current dir is not readable" ) . join ( "target" ) . join ( "debug" ) . join ( "deps" ) ;
151
168
152
169
if let Some ( "clippy" ) = std:: env:: args ( ) . nth ( 1 ) . as_ref ( ) . map ( AsRef :: as_ref) {
153
170
// this arm is executed on the initial call to `cargo clippy`
154
171
155
- if std:: env:: args ( ) . any ( |a| a == "--help" || a == "-h" ) {
156
- show_help ( ) ;
157
- return ;
158
- }
159
-
160
172
let manifest_path_arg = std:: env:: args ( ) . skip ( 2 ) . find ( |val| val. starts_with ( "--manifest-path=" ) ) ;
161
173
162
- let mut metadata = cargo:: metadata ( manifest_path_arg. as_ref ( ) . map ( AsRef :: as_ref) ) . expect ( "could not obtain cargo metadata" ) ;
174
+ let mut metadata = if let Ok ( metadata) = cargo:: metadata ( manifest_path_arg. as_ref ( ) . map ( AsRef :: as_ref) ) {
175
+ metadata
176
+ } else {
177
+ let _ = io:: stderr ( ) . write_fmt ( format_args ! ( "error: Could not obtain cargo metadata." ) ) ;
178
+ process:: exit ( 101 ) ;
179
+ } ;
163
180
164
181
assert_eq ! ( metadata. version, 1 ) ;
165
182
0 commit comments