summary refs log tree commit diff
path: root/compiler/rustc_lint/src/lints.rs
AgeCommit message (Collapse)AuthorLines
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.
2023-11-20Add allow-by-default lint for unit bindings许杰友 Jieyou Xu (Joe)-0/+7
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.
2023-11-18Remove --check-cfg checking of --cfg argsUrgau-15/+0
2023-10-20s/generator/coroutine/Oli Scherer-1/+1
2023-10-20s/Generator/Coroutine/Oli Scherer-1/+1
2023-10-16debug Span::ctxt() call detectionArthur Lafrance-3/+1
2023-10-16basic lint v2 implementedArthur Lafrance-0/+6
2023-10-12Fix duplicate note on internal feature gateGurinder Singh-2/+0
The BuiltinInternalFeatures gate already has a struct level #[note] attribute. The additional note field in it caused a duplicate to be displayed when it was set to Some(...) which happened when the feature had an associated issue
2023-10-06Rollup merge of #116421 - Urgau:inter-mut-invalid_ref_casting, r=oli-obkMatthias Krüger-0/+4
Clarify `invalid_reference_casting` lint around interior mutable types This is PR intends to clarify the `invalid_reference_casting` lint around interior mutable types by adding a note for them saying that they should go through `UnsafeCell::get`. So for this code: ```rust let cell = &std::cell::UnsafeCell::new(0); let _num = &mut *(cell as *const _ as *mut i32); ``` the following note will be added to the lint output: ```diff error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell` --> $DIR/reference_casting.rs:68:16 | LL | let _num = &mut *(cell as *const _ as *mut i32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html> + = note: even for types with interior mutability, the only legal way to obtain a mutable pointer from a shared reference is through `UnsafeCell::get` ``` Suggestion are welcome around the note contents. Fixes https://github.com/rust-lang/rust/issues/116410 cc `@RalfJung`
2023-10-04Clarify `invalid_reference_casting` lint around interior mutable typesUrgau-0/+4
2023-10-03Add async_fn_in_trait lintMichael Goulet-0/+21
2023-09-16Auto merge of #114494 - est31:extend_useless_ptr_null_checks, r=jackh726bors-0/+2
Make useless_ptr_null_checks smarter about some std functions This teaches the `useless_ptr_null_checks` lint that some std functions can't ever return null pointers, because they need to point to valid data, get references as input, etc. This is achieved by introducing an `#[rustc_never_returns_null_ptr]` attribute and adding it to these std functions (gated behind bootstrap `cfg_attr`). Later on, the attribute could maybe be used to tell LLVM that the returned pointer is never null. I don't expect much impact of that though, as the functions are pretty shallow and usually the input data is already never null. Follow-up of PR #113657 Fixes #114442
2023-08-30feat(rustc_lint): make `CheckLintName` respect lint levelWeihang Lo-10/+58
2023-08-24lint: translate `RenamedOrRemovedLint`Weihang Lo-6/+12
2023-08-23Improve note for the invalid_reference_casting lintUrgau-0/+2
Add link to the book interior mutability chapter, https://doc.rust-lang.org/book/ch15-05-interior-mutability.html.
2023-08-06Improve diagnostics and add tests for function callsest31-0/+2
2023-08-06Rollup merge of #114486 - Urgau:const-context-nan-suggestion-114471, ↵Matthias Krüger-1/+1
r=compiler-errors Avoid invalid NaN lint machine-applicable suggestion in const context This PR removes the machine-applicable suggestion in const context for the `invalid_nan_comparision` lint ~~and replace it with a simple help~~. Fixes https://github.com/rust-lang/rust/issues/114471
2023-08-05Avoid invalid NaN lint machine-applicable suggestion in const contextUrgau-1/+1
2023-08-04Auto merge of #112117 - bryangarza:track-caller-feature-gate, r=compiler-errorsbors-1/+1
Add separate feature gate for async fn track caller This patch adds a feature gate `async_fn_track_caller` that is separate from `closure_track_caller`. This is to allow enabling `async_fn_track_caller` separately. Fixes #110009
2023-08-04Rollup merge of #114472 - estebank:issue-76140, r=compiler-errorsMatthias Krüger-1/+3
Reword `confusable_idents` lint Fix #76140.
2023-08-04Reword confusable idents lintEsteban Küber-1/+3
Fix #76140.
2023-08-04Auto merge of #114414 - cjgillot:early-unnameable-test, r=petrochenkovbors-4/+0
Make test harness lint about unnnameable tests. Implementation of https://github.com/rust-lang/rust/pull/113734#discussion_r1283073418 About the options suggested in https://github.com/rust-lang/rust/issues/36629#issuecomment-404753945: adding this case to unused_attribute was just more complicated. I'll try to understand a bit more what you had in mind in https://github.com/rust-lang/rfcs/pull/2471#issuecomment-397241123 This was just simpler to do in a standalone PR. I'll remove the corresponding changes from https://github.com/rust-lang/rust/pull/113734 later. r? `@petrochenkov`
2023-08-03Auto merge of #108955 - Nilstrieb:dont-use-me-pls, r=oli-obkbors-2/+11
Add `internal_features` lint Implements https://github.com/rust-lang/compiler-team/issues/596 Also requires some more test blessing for codegen tests etc `@jyn514` had the idea of just `allow`ing the lint by default in the test suite. I'm not sure whether this is a good idea, but it's definitely one worth considering. Additional input encouraged.
2023-08-03Rollup merge of #113657 - Urgau:expand-incorrect_fn_null_check-lint, r=cjgillotMatthias Krüger-4/+16
Expand, rename and improve `incorrect_fn_null_checks` lint This PR, - firstly, expand the lint by now linting on references - secondly, it renames the lint `incorrect_fn_null_checks` -> `useless_ptr_null_checks` - and thirdly it improves the lint by catching `ptr::from_mut`, `ptr::from_ref`, as well as `<*mut _>::cast` and `<*const _>::cast_mut` Fixes https://github.com/rust-lang/rust/issues/113601 cc ```@est31```
2023-08-03Make test harness lint about unnnameable tests.Camille GILLOT-4/+0
2023-08-03Add `internal_features` lintNilstrieb-2/+11
It lints against features that are inteded to be internal to the compiler and standard library. Implements MCP #596. We allow `internal_features` in the standard library and compiler as those use many features and this _is_ the standard library from the "internal to the compiler and standard library" after all. Marking some features as internal wasn't exactly the most scientific approach, I just marked some mostly obvious features. While there is a categorization in the macro, it's not very well upheld (should probably be fixed in another PR). We always pass `-Ainternal_features` in the testsuite About 400 UI tests and several other tests use internal features. Instead of throwing the attribute on each one, just always allow them. There's nothing wrong with testing internal features^^
2023-08-03Also add label with original type for function pointersUrgau-1/+5
2023-08-02Add separate feature gate for async fn track callerBryan Garza-1/+1
This patch adds a feature gate `async_fn_track_caller` that is separate from `closure_track_caller`. This is to allow enabling `async_fn_track_caller` separately. Fixes #110009
2023-08-01Rename incorrect_fn_null_checks to useless_ptr_null_checksUrgau-4/+4
2023-08-01Expand incorrect_fn_null_check lint with reference null checkingUrgau-3/+11
2023-07-29Improve diagnostics of the invalid_reference_casting lintUrgau-4/+11
2023-07-29Add support for deferred casting for the invalid_reference_casting lintUrgau-1/+4