@@ -25,7 +25,9 @@ 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:: {
29+ analyze_cargo_lints_table, blanket_hint_mostly_unused, check_im_a_teapot,
30+ } ;
2931use crate :: util:: toml:: { InheritableFields , read_manifest} ;
3032use crate :: util:: {
3133 Filesystem , GlobalContext , IntoUrl , context:: CargoResolverConfig , context:: ConfigRelativePath ,
@@ -409,10 +411,7 @@ impl<'gctx> Workspace<'gctx> {
409411 }
410412
411413 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 ( )
416415 }
417416
418417 /// Returns the root path of this workspace.
@@ -907,10 +906,7 @@ impl<'gctx> Workspace<'gctx> {
907906
908907 /// Returns the unstable nightly-only features enabled via `cargo-features` in the manifest.
909908 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 ( )
914910 }
915911
916912 pub fn resolve_behavior ( & self ) -> ResolveBehavior {
@@ -1206,10 +1202,20 @@ impl<'gctx> Workspace<'gctx> {
12061202
12071203 pub fn emit_warnings ( & self ) -> CargoResult < ( ) > {
12081204 let mut first_emitted_error = None ;
1205+
1206+ let cli_unstable = self . gctx . cli_unstable ( ) ;
1207+ if cli_unstable. cargo_lints || cli_unstable. profile_hint_mostly_unused {
1208+ if let Err ( e) = self . emit_ws_lints ( )
1209+ && first_emitted_error. is_none ( )
1210+ {
1211+ first_emitted_error = Some ( e) ;
1212+ }
1213+ }
1214+
12091215 for ( path, maybe_pkg) in & self . packages . packages {
12101216 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)
1217+ if cli_unstable. cargo_lints {
1218+ if let Err ( e) = self . emit_pkg_lints ( pkg, & path)
12131219 && first_emitted_error. is_none ( )
12141220 {
12151221 first_emitted_error = Some ( e) ;
@@ -1248,7 +1254,7 @@ impl<'gctx> Workspace<'gctx> {
12481254 }
12491255 }
12501256
1251- pub fn emit_lints ( & self , pkg : & Package , path : & Path ) -> CargoResult < ( ) > {
1257+ pub fn emit_pkg_lints ( & self , pkg : & Package , path : & Path ) -> CargoResult < ( ) > {
12521258 let mut error_count = 0 ;
12531259 let toml_lints = pkg
12541260 . manifest ( )
@@ -1262,15 +1268,9 @@ impl<'gctx> Workspace<'gctx> {
12621268 . cloned ( )
12631269 . unwrap_or ( manifest:: TomlToolLints :: default ( ) ) ;
12641270
1265- let ws_contents = match self . root_maybe ( ) {
1266- MaybePackage :: Package ( pkg) => pkg. manifest ( ) . contents ( ) ,
1267- MaybePackage :: Virtual ( v) => v. contents ( ) ,
1268- } ;
1271+ let ws_contents = self . root_maybe ( ) . contents ( ) ;
12691272
1270- let ws_document = match self . root_maybe ( ) {
1271- MaybePackage :: Package ( pkg) => pkg. manifest ( ) . document ( ) ,
1272- MaybePackage :: Virtual ( v) => v. document ( ) ,
1273- } ;
1273+ let ws_document = self . root_maybe ( ) . document ( ) ;
12741274
12751275 analyze_cargo_lints_table (
12761276 pkg,
@@ -1282,6 +1282,49 @@ impl<'gctx> Workspace<'gctx> {
12821282 self . gctx ,
12831283 ) ?;
12841284 check_im_a_teapot ( pkg, & path, & cargo_lints, & mut error_count, self . gctx ) ?;
1285+
1286+ if error_count > 0 {
1287+ Err ( crate :: util:: errors:: AlreadyPrintedError :: new ( anyhow ! (
1288+ "encountered {error_count} errors(s) while running lints"
1289+ ) )
1290+ . into ( ) )
1291+ } else {
1292+ Ok ( ( ) )
1293+ }
1294+ }
1295+
1296+ pub fn emit_ws_lints ( & self ) -> CargoResult < ( ) > {
1297+ let mut error_count = 0 ;
1298+
1299+ let cargo_lints = match self . root_maybe ( ) {
1300+ MaybePackage :: Package ( pkg) => {
1301+ let toml = pkg. manifest ( ) . normalized_toml ( ) ;
1302+ if let Some ( ws) = & toml. workspace {
1303+ ws. lints . as_ref ( )
1304+ } else {
1305+ toml. lints . as_ref ( ) . map ( |l| & l. lints )
1306+ }
1307+ }
1308+ MaybePackage :: Virtual ( vm) => vm
1309+ . normalized_toml ( )
1310+ . workspace
1311+ . as_ref ( )
1312+ . unwrap ( )
1313+ . lints
1314+ . as_ref ( ) ,
1315+ }
1316+ . and_then ( |t| t. get ( "cargo" ) )
1317+ . cloned ( )
1318+ . unwrap_or ( manifest:: TomlToolLints :: default ( ) ) ;
1319+
1320+ blanket_hint_mostly_unused (
1321+ self . root_maybe ( ) ,
1322+ self . root_manifest ( ) ,
1323+ & cargo_lints,
1324+ & mut error_count,
1325+ self . gctx ,
1326+ ) ?;
1327+
12851328 if error_count > 0 {
12861329 Err ( crate :: util:: errors:: AlreadyPrintedError :: new ( anyhow ! (
12871330 "encountered {error_count} errors(s) while running lints"
@@ -1888,6 +1931,41 @@ impl MaybePackage {
18881931 MaybePackage :: Virtual ( _) => false ,
18891932 }
18901933 }
1934+
1935+ pub fn contents ( & self ) -> & str {
1936+ match self {
1937+ MaybePackage :: Package ( p) => p. manifest ( ) . contents ( ) ,
1938+ MaybePackage :: Virtual ( v) => v. contents ( ) ,
1939+ }
1940+ }
1941+
1942+ pub fn document ( & self ) -> & toml:: Spanned < toml:: de:: DeTable < ' static > > {
1943+ match self {
1944+ MaybePackage :: Package ( p) => p. manifest ( ) . document ( ) ,
1945+ MaybePackage :: Virtual ( v) => v. document ( ) ,
1946+ }
1947+ }
1948+
1949+ pub fn edition ( & self ) -> Edition {
1950+ match self {
1951+ MaybePackage :: Package ( p) => p. manifest ( ) . edition ( ) ,
1952+ MaybePackage :: Virtual ( _) => Edition :: default ( ) ,
1953+ }
1954+ }
1955+
1956+ pub fn profiles ( & self ) -> Option < & TomlProfiles > {
1957+ match self {
1958+ MaybePackage :: Package ( p) => p. manifest ( ) . profiles ( ) ,
1959+ MaybePackage :: Virtual ( v) => v. profiles ( ) ,
1960+ }
1961+ }
1962+
1963+ pub fn unstable_features ( & self ) -> & Features {
1964+ match self {
1965+ MaybePackage :: Package ( p) => p. manifest ( ) . unstable_features ( ) ,
1966+ MaybePackage :: Virtual ( vm) => vm. unstable_features ( ) ,
1967+ }
1968+ }
18911969}
18921970
18931971impl WorkspaceRootConfig {
0 commit comments