Repository: rust
Issue: Diagnostic suggestions sometimes incorrectly point to external crates
Labels: C-bug,A-suggestion-diagnostics,D-invalid-suggestion

In some situations, rustc may provide a diagnostic suggestion in an external crate, which in general it shouldn't do.  These external crates may be in cargo's registry cache, which the user should not be modifying.  For example, `cargo fix` may stomp on changes outside of the package (which is a separate issue https://github.com/rust-lang/cargo/issues/9857).  Usually rustc is good about avoiding this, so I'm not sure what is wrong in these situations.

In general, I expect rustc to not provide suggestions to modify dependencies.

The crater run found several instances of this, each for different underlying reasons (all dealing with macros).

## Example 1 — unused tt

Example dependency:

```rust
// bar.rs
#[macro_export]
macro_rules! m {
    ($i:tt) => {
        $crate::m!(foo $i);
    };
    (foo $i:tt) => {
        let $i = 1;
    }
}
```

Example local crate:

```rust
// foo.rs
pub fn foo() {
    bar::m!(abc);
}
```

Resulting suggestion:

```
warning: unused variable: `abc`
 --> /Users/eric/Temp/crater-2021/foo/bar/src/lib.rs:7:13
  |
7 |         let $i = 1;
  |             ^^ help: if this is intentional, prefix it with an underscore: `_abc`
  |
  = note: `#[warn(unused_variables)]` on by default
```

A key point of this example is that the macro uses `tt` instead of `ident`.  `ident` will not issue an unused warning.

Found in the 2021 crater run for:

* https://tiny.cc/oaaiuz/gh/rchaser53.at-coder-localplayground/log.txt

## Example 2 — Weird $body suggestion

The following makes a suggestion to remove unnecessary braces, but the suggestion doesn't actually remove any braces.

```rust
// Using cpython 0.2.1
use cpython::*;

fn hello(py: Python) -> PyResult<PyString> {
    Ok(PyString::new(py, "Rust says: Hello world"))
}

py_module_initializer!(example, initexample, PyInit_example, |py, m| {
    m.add(py, "hello", py_fn!(py, hello()))?;
    Ok(())
});
```

Suggestion:

```
warning: unnecessary braces around block return value
   --> /Users/eric/.cargo/registry/src/github.com-1ecc6299db9ec823/cpython-0.2.1/src/argparse.rs:386:52
    |
386 |     ( $py:expr, $iter:expr, $body:block, [] ) => { $body };
    |                                                    ^^^^^ help: remove these braces
    |
    = note: `#[warn(unused_braces)]` on by default

warning: `foo` (lib) generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
```

Found in the 2021 crater run for:

* https://tiny.cc/oaaiuz/gh/AustinHaugerud.mb_ext_script/log.txt
* https://tiny.cc/oaaiuz/gh/expobrain.python-rust-library-example/log.txt
* https://tiny.cc/oaaiuz/gh/mazurwiktor.aoaddons-python/log.txt

## Example 3 — Hidden JSON suggestion

In this example, the human-readable text doesn't mention the dependency, but the JSON includes a MachineApplicable suggestion.

```rust
// diesel 1.4.7
#[macro_use]
extern crate diesel;
use diesel::table;

table! {
    use diesel::sql_types::*;
    use std::fs;

    users {
        id -> Integer,
        name -> VarChar,
        favorite_color -> Nullable<VarChar>,
    }
}
```

This emits two duplicate diagnostics in human form:

```
warning: unused import: `std::fs`
 --> src/lib.rs:7:9
  |
7 |     use std::fs;
  |         ^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused import: `std::fs`
 --> src/lib.rs:7:9
  |
7 |     use std::fs;
  |         ^^^^^^^

warning: `foo` (lib) generated 2 warnings
```

Buried in the JSON output is a machine-applicable change which modifies diesel:

```javascript
{
    "byte_end": 9126,
    "byte_start": 9108,
    "column_end": 55,
    "column_start": 37,
    "expansion":
    {},
    "file_name": "/Users/eric/.cargo/registry/src/github.com-1ecc6299db9ec823/diesel-1.4.7/src/macros/mod.rs",
    "is_primary": true,
    "label": null,
    "line_end": 299,
    "line_start": 299,
    "suggested_replacement": "",
    "suggestion_applicability": "MachineApplicable",
    "text":
    [
        {
            "highlight_end": 55,
            "highlight_start": 37,
            "text": "            imports = [$($imports)* use $($import)::+;],"
        }
    ]
}
```

Found in the 2021 crater run for:

* https://tiny.cc/oaaiuz/gh/pigeon-app.pigeon-backend/log.txt

### Meta

`rustc --version --verbose`:
```
rustc 1.56.0-nightly (5eacec9ec 2021-08-28)
binary: rustc
commit-hash: 5eacec9ec7e112a0de1011519a57c45586d58414
commit-date: 2021-08-28
host: x86_64-apple-darwin
release: 1.56.0-nightly
LLVM version: 13.0.0
```