summary refs log tree commit diff
path: root/compiler/rustc_lint/src/lints.rs
AgeCommit message (Collapse)AuthorLines
2024-04-21Pass translation closure to add_to_diag_with() as referenceXiretza-9/+9
2024-03-29Add support for NonNull in ambiguous_wide_ptr_comparisionsUrgau-6/+12
2024-03-29Add detection of [Partial]Ord methods to the ambiguous wide ptr cmp lintUrgau-6/+8
2024-03-11Rename `DecorateLint` as `LintDiagnostic`.Nicholas Nethercote-11/+11
To match `derive(LintDiagnostic)`.
2024-03-11Rename `AddToDiagnostic` as `Subdiagnostic`.Nicholas Nethercote-21/+21
To match `derive(Subdiagnostic)`. Also rename `add_to_diagnostic{,_with}` as `add_to_diag{,_with}`.
2024-03-05Rename `SubdiagnosticMessageOp` as `SubdiagMessageOp`.Nicholas Nethercote-10/+10
2024-03-05Rename `DiagnosticMessage` as `DiagMessage`.Nicholas Nethercote-15/+15
2024-02-28Auto merge of #121489 - nnethercote:diag-renaming, r=davidtwcobors-23/+23
Diagnostic renaming Renaming various diagnostic types from `Diagnostic*` to `Diag*`. Part of https://github.com/rust-lang/compiler-team/issues/722. There are more to do but this is enough for one PR. r? `@davidtwco`
2024-02-28Remove the `UntranslatableDiagnosticTrivial` lint.Nicholas Nethercote-4/+0
It's a specialized form of the `UntranslatableDiagnostic` lint that is deny-by-default. Now that `UntranslatableDiagnostic` has been changed from allow-by-default to deny-by-default, the trivial variant is no longer needed.
2024-02-28Rename `DiagnosticStyledString` as `DiagStyledString`.Nicholas Nethercote-4/+4
2024-02-28Rename `DiagnosticBuilder` as `Diag`.Nicholas Nethercote-20/+20
Much better! Note that this involves renaming (and updating the value of) `DIAGNOSTIC_BUILDER` in clippy.
2024-02-26Rollup merge of #120840 - ↵Guillaume Gomez-0/+2
HTGAzureX1212:HTGAzureX1212/unicode-identifier-types, r=fmease,Manishearth Split Diagnostics for Uncommon Codepoints: Add Individual Identifier Types This pull request further modifies the `uncommon_codepoints` lint, adding the individual identifier types of `Technical`, `Not_NFKC`, `Exclusion` and `Limited_Use` to the diagnostic message. Example rendered diagnostic: ``` error: identifier contains a Unicode codepoint that is not used in normalized strings: 'ij' --> $DIR/lint-uncommon-codepoints.rs:6:4 | LL | fn dijkstra() {} | ^^^^^^^ = note: this character is included in the Not_NFKC Unicode general security profile ``` Second step of #120228.
2024-02-26separate messages for individual categoriesHTGAzureX1212.-0/+2
2024-02-25Auto merge of #120393 - Urgau:rfc3373-non-local-defs, r=WaffleLapkinbors-0/+39
Implement RFC 3373: Avoid non-local definitions in functions This PR implements [RFC 3373: Avoid non-local definitions in functions](https://github.com/rust-lang/rust/issues/120363).
2024-02-23Rollup merge of #121471 - estebank:lint-clone, r=TaKO8KiMatthias Krüger-0/+6
When encountering `<&T as Clone>::clone(x)` because `T: Clone`, suggest `#[derive(Clone)]` CC #40699. ``` warning: call to `.clone()` on a reference in this situation does nothing --> $DIR/noop-method-call.rs:23:71 | LL | let non_clone_type_ref_clone: &PlainType<u32> = non_clone_type_ref.clone(); | ^^^^^^^^ | = note: the type `PlainType<u32>` does not implement `Clone`, so calling `clone` on `&PlainType<u32>` copies the reference, which does not do anything and can be removed help: remove this redundant call | LL - let non_clone_type_ref_clone: &PlainType<u32> = non_clone_type_ref.clone(); LL + let non_clone_type_ref_clone: &PlainType<u32> = non_clone_type_ref; | help: if you meant to clone `PlainType<u32>`, implement `Clone` for it | LL + #[derive(Clone)] LL | struct PlainType<T>(T); | ```
2024-02-22When encountering `<&T as Clone>::clone(x)` because `T: Clone`, suggest ↵Esteban Küber-0/+6
`#[derive(Clone)]` CC #40699.
2024-02-21Rollup merge of #121338 - ↵Dylan DPC-3/+6
jieyouxu:ambiguous_wide_pointer_comparisons_suggestion, r=Nadrieril Downgrade ambiguous_wide_pointer_comparisons suggestions to MaybeIncorrect In certain cases like #121330, it is possible to have more than one suggestion from the `ambiguous_wide_pointer_comparisons` lint (which before this PR are `MachineApplicable`). When this gets passed to rustfix, rustfix makes *multiple* changes according to the suggestions which result in incorrect code. This is a temporary workaround. The real long term solution to problems like these is to address <https://github.com/rust-lang/rust/issues/53934>. This PR also includes a drive-by edit to the panic message emitted by compiletest because "ui" test suite now uses `//`@`` directives. Fixes #121330.
2024-02-20Downgrade ambiguous_wide_pointer_comparisons suggestions to MaybeIncorrect许杰友 Jieyou Xu (Joe)-3/+6
It is possible to have more than one valid suggestion, which when applied together via rustfix causes the code to no longer compile. This is a temporary workaround; the real long term solution to these issues is to solve <https://github.com/rust-lang/rust/issues/53934>.
2024-02-20Rollup merge of #121318 - kadiwa4:no_assembly_in_supposedly_safe_code, ↵Nilstrieb-0/+3
r=Nilstrieb Trigger `unsafe_code` lint on invocations of `global_asm` `unsafe_code` already warns about things that don't involve the `unsafe` keyword, e.g. `#[no_mangle]`. This makes it warn on `core::arch::global_asm` too. Fixes #103078
2024-02-20Reduce capabilities of `Diagnostic`.Nicholas Nethercote-15/+51
Currently many diagnostic modifier methods are available on both `Diagnostic` and `DiagnosticBuilder`. This commit removes most of them from `Diagnostic`. To minimize the diff size, it keeps them within `diagnostic.rs` but changes the surrounding `impl Diagnostic` block to `impl DiagnosticBuilder`. (I intend to move things around later, to give a more sensible code layout.) `Diagnostic` keeps a few methods that it still needs, like `sub`, `arg`, and `replace_args`. The `forward!` macro, which defined two additional methods per call (e.g. `note` and `with_note`), is replaced by the `with_fn!` macro, which defines one additional method per call (e.g. `with_note`). It's now also only used when necessary -- not all modifier methods currently need a `with_*` form. (New ones can be easily added as necessary.) All this also requires changing `trait AddToDiagnostic` so its methods take `DiagnosticBuilder` instead of `Diagnostic`, which leads to many mechanical changes. `SubdiagnosticMessageOp` gains a type parameter `G`. There are three subdiagnostics -- `DelayedAtWithoutNewline`, `DelayedAtWithNewline`, and `InvalidFlushedDelayedDiagnosticLevel` -- that are created within the diagnostics machinery and appended to external diagnostics. These are handled at the `Diagnostic` level, which means it's now hard to construct them via `derive(Diagnostic)`, so instead we construct them by hand. This has no effect on what they look like when printed. There are lots of new `allow` markers for `untranslatable_diagnostics` and `diagnostics_outside_of_impl`. This is because `#[rustc_lint_diagnostics]` annotations were present on the `Diagnostic` modifier methods, but missing from the `DiagnosticBuilder` modifier methods. They're now present.
2024-02-20trigger `unsafe_code` on `global_asm!` invocationsKalle Wachsmuth-0/+3
2024-02-17Rollup merge of #121085 - davidtwco:always-eager-diagnostics, r=nnethercoteMatthias Krüger-1/+1
errors: only eagerly translate subdiagnostics Subdiagnostics don't need to be lazily translated, they can always be eagerly translated. Eager translation is slightly more complex as we need to have a `DiagCtxt` available to perform the translation, which involves slightly more threading of that context. This slight increase in complexity should enable later simplifications - like passing `DiagCtxt` into `AddToDiagnostic` and moving Fluent messages into the diagnostic structs rather than having them in separate files (working on that was what led to this change). r? ```@nnethercote```
2024-02-17Add cargo update suggestion for non local defsUrgau-1/+17
2024-02-17Add const-anon suggestion for non local implUrgau-1/+7
2024-02-17Implement RFC3373 non local definitions lintUrgau-0/+17
2024-02-15errors: only eagerly translate subdiagnosticsDavid Wood-1/+1
Subdiagnostics don't need to be lazily translated, they can always be eagerly translated. Eager translation is slightly more complex as we need to have a `DiagCtxt` available to perform the translation, which involves slightly more threading of that context. This slight increase in complexity should enable later simplifications - like passing `DiagCtxt` into `AddToDiagnostic` and moving Fluent messages into the diagnostic structs rather than having them in separate files (working on that was what led to this change). Signed-off-by: David Wood <david@davidtw.co>
2024-02-15Use generic `NonZero` internally.Markus Reiter-3/+2
2024-02-12Lint on reference casting to bigger underlying allocationUrgau-1/+13
2024-02-09Rollup merge of #120693 - nnethercote:invert-diagnostic-lints, r=davidtwcoMatthias Krüger-1/+2
Invert diagnostic lints. That is, change `diagnostic_outside_of_impl` and `untranslatable_diagnostic` from `allow` to `deny`, because more than half of the compiler has been converted to use translated diagnostics. This commit removes more `deny` attributes than it adds `allow` attributes, which proves that this change is warranted. r? ````@davidtwco````
2024-02-08Add `SubdiagnosticMessageOp` as a trait alias.Nicholas Nethercote-37/+10
It avoids a lot of repetition.
2024-02-06Invert diagnostic lints.Nicholas Nethercote-1/+2
That is, change `diagnostic_outside_of_impl` and `untranslatable_diagnostic` from `allow` to `deny`, because more than half of the compiler has be converted to use translated diagnostics. This commit removes more `deny` attributes than it adds `allow` attributes, which proves that this change is warranted.
2024-01-29Stop using `String` for error codes.Nicholas Nethercote-3/+3
Error codes are integers, but `String` is used everywhere to represent them. Gross! This commit introduces `ErrCode`, an integral newtype for error codes, replacing `String`. It also introduces a constant for every error code, e.g. `E0123`, and removes the `error_code!` macro. The constants are imported wherever used with `use rustc_errors::codes::*`. With the old code, we have three different ways to specify an error code at a use point: ``` error_code!(E0123) // macro call struct_span_code_err!(dcx, span, E0123, "msg"); // bare ident arg to macro call \#[diag(name, code = "E0123")] // string struct Diag; ``` With the new code, they all use the `E0123` constant. ``` E0123 // constant struct_span_code_err!(dcx, span, E0123, "msg"); // constant \#[diag(name, code = E0123)] // constant struct Diag; ``` The commit also changes the structure of the error code definitions: - `rustc_error_codes` now just defines a higher-order macro listing the used error codes and nothing else. - Because that's now the only thing in the `rustc_error_codes` crate, I moved it into the `lib.rs` file and removed the `error_codes.rs` file. - `rustc_errors` uses that macro to define everything, e.g. the error code constants and the `DIAGNOSTIC_TABLES`. This is in its new `codes.rs` file.
2024-01-23address requested changesHTGAzureX1212.-0/+1
2024-01-23add list of characters to uncommon codepoints lintHTGAzureX1212.-1/+3
2024-01-22Revert "Auto merge of #118133 - Urgau:stabilize_trait_upcasting, r=WaffleLapkin"Oli Scherer-1/+0
This reverts commit 6d2b84b3ed7848fd91b8d6151d4451b3103ed816, reversing changes made to 73bc12199ea8c7651ed98b069c0dd6b0bb5fabcf.
2024-01-22Rollup merge of #119710 - Nilstrieb:let-_-=-oops, r=TaKO8KiMatthias Krüger-17/+26
Improve `let_underscore_lock` - lint if the lock was in a nested pattern - lint if the lock is inside a `Result<Lock, _>` addresses https://github.com/rust-lang/rust/pull/119704#discussion_r1444044745
2024-01-13Add check for ui_testing via promoting parameters from `ParseSess` to `Session`George-lewis-3/+3
2024-01-12Improve `let_underscore_lock`Nilstrieb-17/+26
- lint if the lock was in a nested pattern - lint if the lock is inside a `Result<Lock, _>`
2024-01-12check rust lints when an unknown lint is detectedyukang-1/+2
2024-01-09Rollup merge of #119704 - chenyukang:yukang-fix-let_underscore, r=NilstriebMatthias Krüger-1/+3
Fix two variable binding issues in lint let_underscore Fixes #119696 Fixes #119697
2024-01-08Fix 2 variable binding issues in let_underscoreyukang-1/+3
2024-01-03Remove lots of `rustc_errors::` qualifiers in `lints.rs`.Nicholas Nethercote-63/+36
2024-01-03Rename some `Diagnostic` setters.Nicholas Nethercote-12/+12
`Diagnostic` has 40 methods that return `&mut Self` and could be considered setters. Four of them have a `set_` prefix. This doesn't seem necessary for a type that implements the builder pattern. This commit removes the `set_` prefixes on those four methods.
2023-12-16Simplify lint decorator derive tooMichael Goulet-45/+11
2023-12-06Add warn-by-default lint against ambiguous wide pointer comparisonsUrgau-0/+70
2023-11-22Rework supertrait lint once againMichael Goulet-4/+10
2023-11-22Auto merge of #118133 - Urgau:stabilize_trait_upcasting, r=WaffleLapkinbors-4/+2
Stabilize RFC3324 dyn upcasting coercion This PR stabilize the `trait_upcasting` feature, aka https://github.com/rust-lang/rfcs/pull/3324. The FCP was completed here: https://github.com/rust-lang/rust/issues/65991#issuecomment-1817552398. ~~And also remove the `deref_into_dyn_supertrait` lint which is now handled by dyn upcasting coercion.~~ Heavily inspired by https://github.com/rust-lang/rust/pull/101718 Fixes https://github.com/rust-lang/rust/issues/65991
2023-11-22Auto merge of #112380 - jieyouxu:useless-bindings-lint, r=WaffleLapkinbors-0/+7
Add allow-by-default lint for unit bindings ### Example ```rust #![warn(unit_bindings)] macro_rules! owo { () => { let whats_this = (); } } fn main() { // No warning if user explicitly wrote `()` on either side. let expr = (); let () = expr; let _ = (); let _ = expr; //~ WARN binding has unit type let pat = expr; //~ WARN binding has unit type let _pat = expr; //~ WARN binding has unit type // No warning for let bindings with unit type in macro expansions. owo!(); // No warning if user explicitly annotates the unit type on the binding. let pat: () = expr; } ``` outputs ``` warning: binding has unit type `()` --> $DIR/unit-bindings.rs:17:5 | LL | let _ = expr; | ^^^^-^^^^^^^^ | | | this pattern is inferred to be the unit type `()` | note: the lint level is defined here --> $DIR/unit-bindings.rs:3:9 | LL | #![warn(unit_bindings)] | ^^^^^^^^^^^^^ warning: binding has unit type `()` --> $DIR/unit-bindings.rs:18:5 | LL | let pat = expr; | ^^^^---^^^^^^^^ | | | this pattern is inferred to be the unit type `()` warning: binding has unit type `()` --> $DIR/unit-bindings.rs:19:5 | LL | let _pat = expr; | ^^^^----^^^^^^^^ | | | this pattern is inferred to be the unit type `()` warning: 3 warnings emitted ``` This lint is not triggered if any of the following conditions are met: - The user explicitly annotates the binding with the `()` type. - The binding is from a macro expansion. - The user explicitly wrote `let () = init;` - The user explicitly wrote `let pat = ();`. This is allowed for local lifetimes. ### Known Issue It is known that this lint can trigger on some proc-macro generated code whose span returns false for `Span::from_expansion` because e.g. the proc-macro simply forwards user code spans, and otherwise don't have distinguishing syntax context compared to non-macro-generated code. For those kind of proc-macros, I believe the correct way to fix them is to instead emit identifers with span like `Span::mixed_site().located_at(user_span)`. Closes #71432.
2023-11-22Stabilize RFC3324 dyn upcasting coercionUrgau-4/+2
Aka trait_upcasting feature. And also adjust the `deref_into_dyn_supertrait` lint.
2023-11-21Fix `clippy::needless_borrow` in the compilerNilstrieb-1/+1
`x clippy compiler -Aclippy::all -Wclippy::needless_borrow --fix`. Then I had to remove a few unnecessary parens and muts that were exposed now.