@@ -25,7 +25,7 @@ use crate::util::context::FeatureUnification;
2525use crate :: util:: edit_distance;
2626use crate :: util:: errors:: { CargoResult , ManifestError } ;
2727use crate :: util:: interning:: InternedString ;
28- use crate :: util:: lints:: { analyze_cargo_lints_table, check_im_a_teapot} ;
28+ use crate :: util:: lints:: { analyze_cargo_lints_table, check_im_a_teapot, global_mostly_unused } ;
2929use crate :: util:: toml:: { InheritableFields , read_manifest} ;
3030use crate :: util:: {
3131 Filesystem , GlobalContext , IntoUrl , context:: CargoResolverConfig , context:: ConfigRelativePath ,
@@ -409,10 +409,7 @@ impl<'gctx> Workspace<'gctx> {
409409 }
410410
411411 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- }
412+ self . root_maybe ( ) . profiles ( )
416413 }
417414
418415 /// Returns the root path of this workspace.
@@ -907,10 +904,7 @@ impl<'gctx> Workspace<'gctx> {
907904
908905 /// Returns the unstable nightly-only features enabled via `cargo-features` in the manifest.
909906 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- }
907+ self . root_maybe ( ) . unstable_features ( )
914908 }
915909
916910 pub fn resolve_behavior ( & self ) -> ResolveBehavior {
@@ -1206,10 +1200,20 @@ impl<'gctx> Workspace<'gctx> {
12061200
12071201 pub fn emit_warnings ( & self ) -> CargoResult < ( ) > {
12081202 let mut first_emitted_error = None ;
1203+
1204+ let cli_unstable = self . gctx . cli_unstable ( ) ;
1205+ if cli_unstable. cargo_lints || cli_unstable. profile_hint_mostly_unused {
1206+ if let Err ( e) = self . emit_ws_lints ( )
1207+ && first_emitted_error. is_none ( )
1208+ {
1209+ first_emitted_error = Some ( e) ;
1210+ }
1211+ }
1212+
12091213 for ( path, maybe_pkg) in & self . packages . packages {
12101214 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)
1215+ if cli_unstable. cargo_lints {
1216+ if let Err ( e) = self . emit_pkg_lints ( pkg, & path)
12131217 && first_emitted_error. is_none ( )
12141218 {
12151219 first_emitted_error = Some ( e) ;
@@ -1248,7 +1252,7 @@ impl<'gctx> Workspace<'gctx> {
12481252 }
12491253 }
12501254
1251- pub fn emit_lints ( & self , pkg : & Package , path : & Path ) -> CargoResult < ( ) > {
1255+ pub fn emit_pkg_lints ( & self , pkg : & Package , path : & Path ) -> CargoResult < ( ) > {
12521256 let mut error_count = 0 ;
12531257 let toml_lints = pkg
12541258 . manifest ( )
@@ -1262,15 +1266,9 @@ impl<'gctx> Workspace<'gctx> {
12621266 . cloned ( )
12631267 . unwrap_or ( manifest:: TomlToolLints :: default ( ) ) ;
12641268
1265- let ws_contents = match self . root_maybe ( ) {
1266- MaybePackage :: Package ( pkg) => pkg. manifest ( ) . contents ( ) ,
1267- MaybePackage :: Virtual ( v) => v. contents ( ) ,
1268- } ;
1269+ let ws_contents = self . root_maybe ( ) . contents ( ) ;
12691270
1270- let ws_document = match self . root_maybe ( ) {
1271- MaybePackage :: Package ( pkg) => pkg. manifest ( ) . document ( ) ,
1272- MaybePackage :: Virtual ( v) => v. document ( ) ,
1273- } ;
1271+ let ws_document = self . root_maybe ( ) . document ( ) ;
12741272
12751273 analyze_cargo_lints_table (
12761274 pkg,
@@ -1282,6 +1280,49 @@ impl<'gctx> Workspace<'gctx> {
12821280 self . gctx ,
12831281 ) ?;
12841282 check_im_a_teapot ( pkg, & path, & cargo_lints, & mut error_count, self . gctx ) ?;
1283+
1284+ if error_count > 0 {
1285+ Err ( crate :: util:: errors:: AlreadyPrintedError :: new ( anyhow ! (
1286+ "encountered {error_count} errors(s) while running lints"
1287+ ) )
1288+ . into ( ) )
1289+ } else {
1290+ Ok ( ( ) )
1291+ }
1292+ }
1293+
1294+ pub fn emit_ws_lints ( & self ) -> CargoResult < ( ) > {
1295+ let mut error_count = 0 ;
1296+
1297+ let cargo_lints = match self . root_maybe ( ) {
1298+ MaybePackage :: Package ( pkg) => {
1299+ let toml = pkg. manifest ( ) . normalized_toml ( ) ;
1300+ if let Some ( ws) = & toml. workspace {
1301+ ws. lints . as_ref ( )
1302+ } else {
1303+ toml. lints . as_ref ( ) . map ( |l| & l. lints )
1304+ }
1305+ }
1306+ MaybePackage :: Virtual ( vm) => vm
1307+ . normalized_toml ( )
1308+ . workspace
1309+ . as_ref ( )
1310+ . unwrap ( )
1311+ . lints
1312+ . as_ref ( ) ,
1313+ }
1314+ . and_then ( |t| t. get ( "cargo" ) )
1315+ . cloned ( )
1316+ . unwrap_or ( manifest:: TomlToolLints :: default ( ) ) ;
1317+
1318+ global_mostly_unused (
1319+ self . root_maybe ( ) ,
1320+ self . root_manifest ( ) ,
1321+ & cargo_lints,
1322+ & mut error_count,
1323+ self . gctx ,
1324+ ) ?;
1325+
12851326 if error_count > 0 {
12861327 Err ( crate :: util:: errors:: AlreadyPrintedError :: new ( anyhow ! (
12871328 "encountered {error_count} errors(s) while running lints"
@@ -1888,6 +1929,41 @@ impl MaybePackage {
18881929 MaybePackage :: Virtual ( _) => false ,
18891930 }
18901931 }
1932+
1933+ pub fn contents ( & self ) -> & str {
1934+ match self {
1935+ MaybePackage :: Package ( p) => p. manifest ( ) . contents ( ) ,
1936+ MaybePackage :: Virtual ( v) => v. contents ( ) ,
1937+ }
1938+ }
1939+
1940+ pub fn document ( & self ) -> & toml:: Spanned < toml:: de:: DeTable < ' static > > {
1941+ match self {
1942+ MaybePackage :: Package ( p) => p. manifest ( ) . document ( ) ,
1943+ MaybePackage :: Virtual ( v) => v. document ( ) ,
1944+ }
1945+ }
1946+
1947+ pub fn edition ( & self ) -> Edition {
1948+ match self {
1949+ MaybePackage :: Package ( p) => p. manifest ( ) . edition ( ) ,
1950+ MaybePackage :: Virtual ( _) => Edition :: default ( ) ,
1951+ }
1952+ }
1953+
1954+ pub fn profiles ( & self ) -> Option < & TomlProfiles > {
1955+ match self {
1956+ MaybePackage :: Package ( p) => p. manifest ( ) . profiles ( ) ,
1957+ MaybePackage :: Virtual ( v) => v. profiles ( ) ,
1958+ }
1959+ }
1960+
1961+ pub fn unstable_features ( & self ) -> & Features {
1962+ match self {
1963+ MaybePackage :: Package ( p) => p. manifest ( ) . unstable_features ( ) ,
1964+ MaybePackage :: Virtual ( vm) => vm. unstable_features ( ) ,
1965+ }
1966+ }
18911967}
18921968
18931969impl WorkspaceRootConfig {
0 commit comments