@@ -24,6 +24,7 @@ use rustc_session::lint::builtin::{
2424 MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS ,
2525} ;
2626use rustc_session:: lint:: { AmbiguityErrorDiag , BuiltinLintDiag } ;
27+ use rustc_session:: utils:: was_invoked_from_cargo;
2728use rustc_span:: edit_distance:: find_best_match_for_name;
2829use rustc_span:: edition:: Edition ;
2930use rustc_span:: hygiene:: MacroKind ;
@@ -2045,10 +2046,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20452046 self . current_crate_outer_attr_insert_span,
20462047 format!( "extern crate {ident};\n " ) ,
20472048 ) ] ,
2048- format ! (
2049- "if you wanted to use a crate named `{ident}`, use `cargo add {ident}` \
2050- to add it to your `Cargo.toml` and import it in your code",
2051- ) ,
2049+ if was_invoked_from_cargo ( ) {
2050+ format ! (
2051+ "if you wanted to use a crate named `{ident}`, use `cargo add {ident}` \
2052+ to add it to your `Cargo.toml` and import it in your code",
2053+ )
2054+ } else {
2055+ format ! (
2056+ "you might be missing a crate named `{ident}`, add it to your \
2057+ project and import it in your code",
2058+ )
2059+ } ,
20522060 Applicability :: MaybeIncorrect ,
20532061 ) ) ,
20542062 )
@@ -2227,14 +2235,24 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
22272235 let descr = binding. res ( ) . descr ( ) ;
22282236 ( format ! ( "{descr} `{ident}` is not a crate or module" ) , suggestion)
22292237 } else {
2230- let suggestion = suggestion. or ( Some ( (
2231- vec ! [ ] ,
2232- format ! (
2233- "if you wanted to use a crate named `{ident}`, use `cargo add {ident}` to \
2234- add it to your `Cargo.toml`",
2235- ) ,
2236- Applicability :: MaybeIncorrect ,
2237- ) ) ) ;
2238+ let suggestion = if suggestion. is_some ( ) {
2239+ suggestion
2240+ } else if was_invoked_from_cargo ( ) {
2241+ Some ( (
2242+ vec ! [ ] ,
2243+ format ! (
2244+ "if you wanted to use a crate named `{ident}`, use `cargo add {ident}` \
2245+ to add it to your `Cargo.toml`",
2246+ ) ,
2247+ Applicability :: MaybeIncorrect ,
2248+ ) )
2249+ } else {
2250+ Some ( (
2251+ vec ! [ ] ,
2252+ format ! ( "you might be missing a crate named `{ident}`" , ) ,
2253+ Applicability :: MaybeIncorrect ,
2254+ ) )
2255+ } ;
22382256 ( format ! ( "use of unresolved module or unlinked crate `{ident}`" ) , suggestion)
22392257 }
22402258 }
0 commit comments