@@ -24,6 +24,7 @@ use rustc_session::lint::builtin::{
24
24
MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS ,
25
25
} ;
26
26
use rustc_session:: lint:: { AmbiguityErrorDiag , BuiltinLintDiag } ;
27
+ use rustc_session:: utils:: was_invoked_from_cargo;
27
28
use rustc_span:: edit_distance:: find_best_match_for_name;
28
29
use rustc_span:: edition:: Edition ;
29
30
use rustc_span:: hygiene:: MacroKind ;
@@ -2045,10 +2046,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
2045
2046
self . current_crate_outer_attr_insert_span,
2046
2047
format!( "extern crate {ident};\n " ) ,
2047
2048
) ] ,
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
+ } ,
2052
2060
Applicability :: MaybeIncorrect ,
2053
2061
) ) ,
2054
2062
)
@@ -2227,14 +2235,24 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
2227
2235
let descr = binding. res ( ) . descr ( ) ;
2228
2236
( format ! ( "{descr} `{ident}` is not a crate or module" ) , suggestion)
2229
2237
} 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
+ } ;
2238
2256
( format ! ( "use of unresolved module or unlinked crate `{ident}`" ) , suggestion)
2239
2257
}
2240
2258
}
0 commit comments