@@ -25,7 +25,9 @@ use crate::util::context::FeatureUnification;
25
25
use crate :: util:: edit_distance;
26
26
use crate :: util:: errors:: { CargoResult , ManifestError } ;
27
27
use crate :: util:: interning:: InternedString ;
28
- use crate :: util:: lints:: { analyze_cargo_lints_table, check_im_a_teapot} ;
28
+ use crate :: util:: lints:: {
29
+ analyze_cargo_lints_table, blanket_hint_mostly_unused, check_im_a_teapot,
30
+ } ;
29
31
use crate :: util:: toml:: { InheritableFields , read_manifest} ;
30
32
use crate :: util:: {
31
33
Filesystem , GlobalContext , IntoUrl , context:: CargoResolverConfig , context:: ConfigRelativePath ,
@@ -409,10 +411,7 @@ impl<'gctx> Workspace<'gctx> {
409
411
}
410
412
411
413
pub fn profiles ( & self ) -> Option < & TomlProfiles > {
412
- match self . root_maybe ( ) {
413
- MaybePackage :: Package ( p) => p. manifest ( ) . profiles ( ) ,
414
- MaybePackage :: Virtual ( vm) => vm. profiles ( ) ,
415
- }
414
+ self . root_maybe ( ) . profiles ( )
416
415
}
417
416
418
417
/// Returns the root path of this workspace.
@@ -907,10 +906,7 @@ impl<'gctx> Workspace<'gctx> {
907
906
908
907
/// Returns the unstable nightly-only features enabled via `cargo-features` in the manifest.
909
908
pub fn unstable_features ( & self ) -> & Features {
910
- match self . root_maybe ( ) {
911
- MaybePackage :: Package ( p) => p. manifest ( ) . unstable_features ( ) ,
912
- MaybePackage :: Virtual ( vm) => vm. unstable_features ( ) ,
913
- }
909
+ self . root_maybe ( ) . unstable_features ( )
914
910
}
915
911
916
912
pub fn resolve_behavior ( & self ) -> ResolveBehavior {
@@ -1206,14 +1202,17 @@ impl<'gctx> Workspace<'gctx> {
1206
1202
1207
1203
pub fn emit_warnings ( & self ) -> CargoResult < ( ) > {
1208
1204
let mut first_emitted_error = None ;
1205
+
1206
+ if let Err ( e) = self . emit_ws_lints ( ) {
1207
+ first_emitted_error = Some ( e) ;
1208
+ }
1209
+
1209
1210
for ( path, maybe_pkg) in & self . packages . packages {
1210
1211
if let MaybePackage :: Package ( pkg) = maybe_pkg {
1211
- if self . gctx . cli_unstable ( ) . cargo_lints {
1212
- if let Err ( e) = self . emit_lints ( pkg, & path)
1213
- && first_emitted_error. is_none ( )
1214
- {
1215
- first_emitted_error = Some ( e) ;
1216
- }
1212
+ if let Err ( e) = self . emit_pkg_lints ( pkg, & path)
1213
+ && first_emitted_error. is_none ( )
1214
+ {
1215
+ first_emitted_error = Some ( e) ;
1217
1216
}
1218
1217
}
1219
1218
let warnings = match maybe_pkg {
@@ -1248,7 +1247,7 @@ impl<'gctx> Workspace<'gctx> {
1248
1247
}
1249
1248
}
1250
1249
1251
- pub fn emit_lints ( & self , pkg : & Package , path : & Path ) -> CargoResult < ( ) > {
1250
+ pub fn emit_pkg_lints ( & self , pkg : & Package , path : & Path ) -> CargoResult < ( ) > {
1252
1251
let mut error_count = 0 ;
1253
1252
let toml_lints = pkg
1254
1253
. manifest ( )
@@ -1262,26 +1261,74 @@ impl<'gctx> Workspace<'gctx> {
1262
1261
. cloned ( )
1263
1262
. unwrap_or ( manifest:: TomlToolLints :: default ( ) ) ;
1264
1263
1265
- let ws_contents = match self . root_maybe ( ) {
1266
- MaybePackage :: Package ( pkg) => pkg. manifest ( ) . contents ( ) ,
1267
- MaybePackage :: Virtual ( v) => v. contents ( ) ,
1268
- } ;
1264
+ let ws_contents = self . root_maybe ( ) . contents ( ) ;
1269
1265
1270
- let ws_document = match self . root_maybe ( ) {
1271
- MaybePackage :: Package ( pkg) => pkg. manifest ( ) . document ( ) ,
1272
- MaybePackage :: Virtual ( v) => v. document ( ) ,
1273
- } ;
1266
+ let ws_document = self . root_maybe ( ) . document ( ) ;
1267
+
1268
+ if self . gctx . cli_unstable ( ) . cargo_lints {
1269
+ analyze_cargo_lints_table (
1270
+ pkg,
1271
+ & path,
1272
+ & cargo_lints,
1273
+ ws_contents,
1274
+ ws_document,
1275
+ self . root_manifest ( ) ,
1276
+ self . gctx ,
1277
+ ) ?;
1278
+ check_im_a_teapot ( pkg, & path, & cargo_lints, & mut error_count, self . gctx ) ?;
1279
+ }
1280
+
1281
+ if error_count > 0 {
1282
+ Err ( crate :: util:: errors:: AlreadyPrintedError :: new ( anyhow ! (
1283
+ "encountered {error_count} errors(s) while running lints"
1284
+ ) )
1285
+ . into ( ) )
1286
+ } else {
1287
+ Ok ( ( ) )
1288
+ }
1289
+ }
1290
+
1291
+ pub fn emit_ws_lints ( & self ) -> CargoResult < ( ) > {
1292
+ let mut error_count = 0 ;
1293
+
1294
+ let cargo_lints = match self . root_maybe ( ) {
1295
+ MaybePackage :: Package ( pkg) => {
1296
+ let toml = pkg. manifest ( ) . normalized_toml ( ) ;
1297
+ if let Some ( ws) = & toml. workspace {
1298
+ ws. lints . as_ref ( )
1299
+ } else {
1300
+ toml. lints . as_ref ( ) . map ( |l| & l. lints )
1301
+ }
1302
+ }
1303
+ MaybePackage :: Virtual ( vm) => vm
1304
+ . normalized_toml ( )
1305
+ . workspace
1306
+ . as_ref ( )
1307
+ . unwrap ( )
1308
+ . lints
1309
+ . as_ref ( ) ,
1310
+ }
1311
+ . and_then ( |t| t. get ( "cargo" ) )
1312
+ . cloned ( )
1313
+ . unwrap_or ( manifest:: TomlToolLints :: default ( ) ) ;
1314
+
1315
+ if self . gctx . cli_unstable ( ) . cargo_lints {
1316
+ // Calls to lint functions go in here
1317
+ }
1318
+
1319
+ // This is a short term hack to allow `blanket_hint_mostly_unused`
1320
+ // to run without requiring `-Zcargo-lints`, which should hopefully
1321
+ // improve the testing expierience while we are collecting feedback
1322
+ if self . gctx . cli_unstable ( ) . profile_hint_mostly_unused {
1323
+ blanket_hint_mostly_unused (
1324
+ self . root_maybe ( ) ,
1325
+ self . root_manifest ( ) ,
1326
+ & cargo_lints,
1327
+ & mut error_count,
1328
+ self . gctx ,
1329
+ ) ?;
1330
+ }
1274
1331
1275
- analyze_cargo_lints_table (
1276
- pkg,
1277
- & path,
1278
- & cargo_lints,
1279
- ws_contents,
1280
- ws_document,
1281
- self . root_manifest ( ) ,
1282
- self . gctx ,
1283
- ) ?;
1284
- check_im_a_teapot ( pkg, & path, & cargo_lints, & mut error_count, self . gctx ) ?;
1285
1332
if error_count > 0 {
1286
1333
Err ( crate :: util:: errors:: AlreadyPrintedError :: new ( anyhow ! (
1287
1334
"encountered {error_count} errors(s) while running lints"
@@ -1888,6 +1935,41 @@ impl MaybePackage {
1888
1935
MaybePackage :: Virtual ( _) => false ,
1889
1936
}
1890
1937
}
1938
+
1939
+ pub fn contents ( & self ) -> & str {
1940
+ match self {
1941
+ MaybePackage :: Package ( p) => p. manifest ( ) . contents ( ) ,
1942
+ MaybePackage :: Virtual ( v) => v. contents ( ) ,
1943
+ }
1944
+ }
1945
+
1946
+ pub fn document ( & self ) -> & toml:: Spanned < toml:: de:: DeTable < ' static > > {
1947
+ match self {
1948
+ MaybePackage :: Package ( p) => p. manifest ( ) . document ( ) ,
1949
+ MaybePackage :: Virtual ( v) => v. document ( ) ,
1950
+ }
1951
+ }
1952
+
1953
+ pub fn edition ( & self ) -> Edition {
1954
+ match self {
1955
+ MaybePackage :: Package ( p) => p. manifest ( ) . edition ( ) ,
1956
+ MaybePackage :: Virtual ( _) => Edition :: default ( ) ,
1957
+ }
1958
+ }
1959
+
1960
+ pub fn profiles ( & self ) -> Option < & TomlProfiles > {
1961
+ match self {
1962
+ MaybePackage :: Package ( p) => p. manifest ( ) . profiles ( ) ,
1963
+ MaybePackage :: Virtual ( v) => v. profiles ( ) ,
1964
+ }
1965
+ }
1966
+
1967
+ pub fn unstable_features ( & self ) -> & Features {
1968
+ match self {
1969
+ MaybePackage :: Package ( p) => p. manifest ( ) . unstable_features ( ) ,
1970
+ MaybePackage :: Virtual ( vm) => vm. unstable_features ( ) ,
1971
+ }
1972
+ }
1891
1973
}
1892
1974
1893
1975
impl WorkspaceRootConfig {
0 commit comments