rust-lang / cargo

The Rust package manager
https://doc.rust-lang.org/cargo
Apache License 2.0
12.87k stars 2.43k forks source link

Lint error from `-Zlints` didn't point to `[lints]` but rustc command line flags #12534

Open weihanglo opened 1 year ago

weihanglo commented 1 year ago

Problem

The current implementation of -Zlints is just a transform from [lints] table to a series of rustc lint flags. When a lint error/warning is emitted, rustc report as if the lint rule were requested on the command line:

   Compiling bad-zlints-error v0.1.0 (/users/weihanglo/bad-zlints-error)
error: unknown lint: `new_lint`
 --> src/lib.rs:1:9
  |
1 | #[allow(new_lint)]
  |         ^^^^^^^^
  |
  = note: requested on the command line with `-D unknown-lints`

error: could not compile `bad-zlints-error` (lib) due to previous error

Steps

# Cargo.toml
[package]
name = "bad-zlints-error"
version = "0.1.0"
edition = "2021"

[lints.rust]
unknown_lints = "deny"
// src/lib.rs
#[allow(new_lint)]

And run cargo +nightly c -Zlints

Possible Solution(s)

Cargo hints rustc that these lint args are from [lints] table. However, this solution has some difficulties:

Notes

This is a known issue around rustflags in Cargo and shouldn't be a blocker for stabilization.

Version

cargo 1.73.0-nightly (7c3904d6c 2023-08-14)
release: 1.73.0-nightly
commit-hash: 7c3904d6c3ed54e8a413023519b55a536ad44d5b
commit-date: 2023-08-14
epage commented 1 year ago

This was acknowledged in the RFC

The potential solution mentioned there was

It would be nice if it could also point to Cargo.toml for this. This could be as simple as a --lint-source=Cargo.toml with rustc knowing just enough about the [lints] table to process it directly.

I know we also talked about a more complex CLI for lints for dealing with priority and other problems, we could potentially do some fancy stuff in there so no Cargo.toml knowledge is needed.

kornelski commented 8 months ago

Related papercut here is that command-line lint names use -, but Cargo.toml and #[warn()] use lint names with _.

This discrepancy is annoying, because copying the lint name from the error message results in an invalid syntax like #[allow(unused-variables)].

epage commented 8 months ago

Cargo.toml and the CLI support both.

rustc normalizes - to _ when reading. When reporting to users, it turns _ to - for the CLI but keeps _ for attributes.