rust/compiler
bors 73bc12199e Auto merge of #112380 - jieyouxu:useless-bindings-lint, r=WaffleLapkin
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-22 14:03:16 +00:00
..
rustc Clean up rustc_*/Cargo.toml. 2023-10-30 08:46:02 +11:00
rustc_abi Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
rustc_arena Bump cfg(bootstrap)s 2023-11-15 19:41:28 -05:00
rustc_ast Replace no_ord_impl with orderable. 2023-11-22 18:38:17 +11:00
rustc_ast_lowering Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
rustc_ast_passes Auto merge of #117928 - nnethercote:rustc_ast_pretty, r=fee1-dead 2023-11-22 05:09:33 +00:00
rustc_ast_pretty Auto merge of #117928 - nnethercote:rustc_ast_pretty, r=fee1-dead 2023-11-22 05:09:33 +00:00
rustc_attr Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
rustc_baked_icu_data Bump cfg(bootstrap)s 2023-11-15 19:41:28 -05:00
rustc_borrowck Replace no_ord_impl with orderable. 2023-11-22 18:38:17 +11:00
rustc_builtin_macros Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
rustc_codegen_cranelift Merge commit 'def04540a4e2541b995195c752c751295606a388' into sync_cg_clif-2023-11-16 2023-11-16 21:15:07 +00:00
rustc_codegen_gcc Pass TyCtxt by value 2023-11-19 14:04:40 -05:00
rustc_codegen_llvm Replace custom_encodable with encodable. 2023-11-22 18:37:14 +11:00
rustc_codegen_ssa Auto merge of #117928 - nnethercote:rustc_ast_pretty, r=fee1-dead 2023-11-22 05:09:33 +00:00
rustc_const_eval Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
rustc_data_structures Replace no_ord_impl with orderable. 2023-11-22 18:38:17 +11:00
rustc_driver Bump cfg(bootstrap)s 2023-11-15 19:41:28 -05:00
rustc_driver_impl Merge Queries::{ongoing_codegen,linker}. 2023-11-22 13:22:49 +11:00
rustc_error_codes Bump cfg(bootstrap)s 2023-11-15 19:41:28 -05:00
rustc_error_messages Bump cfg(bootstrap)s 2023-11-15 19:41:28 -05:00
rustc_errors Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
rustc_expand Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
rustc_feature Bump cfg(bootstrap)s 2023-11-15 19:41:28 -05:00
rustc_fluent_macro Bump cfg(bootstrap)s 2023-11-15 19:41:28 -05:00
rustc_fs_util Clean up rustc_*/Cargo.toml. 2023-10-30 08:46:02 +11:00
rustc_graphviz Bump cfg(bootstrap)s 2023-11-15 19:41:28 -05:00
rustc_hir Replace no_ord_impl with orderable. 2023-11-22 18:38:17 +11:00
rustc_hir_analysis Replace no_ord_impl with orderable. 2023-11-22 18:38:17 +11:00
rustc_hir_pretty Auto merge of #117928 - nnethercote:rustc_ast_pretty, r=fee1-dead 2023-11-22 05:09:33 +00:00
rustc_hir_typeck Replace no_ord_impl with orderable. 2023-11-22 18:38:17 +11:00
rustc_incremental Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
rustc_index Replace no_ord_impl with orderable. 2023-11-22 18:38:17 +11:00
rustc_index_macros Document newtype_index attributes. 2023-11-22 18:38:20 +11:00
rustc_infer Replace no_ord_impl with orderable. 2023-11-22 18:38:17 +11:00
rustc_interface Auto merge of #118086 - nnethercote:queries-cleanups, r=bjorn3 2023-11-22 11:44:56 +00:00
rustc_lexer Clean up rustc_*/Cargo.toml. 2023-10-30 08:46:02 +11:00
rustc_lint Auto merge of #112380 - jieyouxu:useless-bindings-lint, r=WaffleLapkin 2023-11-22 14:03:16 +00:00
rustc_lint_defs Remove --check-cfg checking of --cfg args 2023-11-18 12:21:58 +01:00
rustc_llvm Auto merge of #117875 - Mark-Simulacrum:bootstrap-bump, r=clubby789 2023-11-16 12:45:27 +00:00
rustc_log rustc_log: provide a way to init logging based on the values, not names, of the env vars 2023-11-11 15:24:33 +01:00
rustc_macros Unify HashStable implementations 2023-11-21 05:49:45 +00:00
rustc_metadata Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
rustc_middle Replace no_ord_impl with orderable. 2023-11-22 18:38:17 +11:00
rustc_mir_build Replace no_ord_impl with orderable. 2023-11-22 18:38:17 +11:00
rustc_mir_dataflow Replace no_ord_impl with orderable. 2023-11-22 18:38:17 +11:00
rustc_mir_transform Replace no_ord_impl with orderable. 2023-11-22 18:38:17 +11:00
rustc_monomorphize Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
rustc_parse Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
rustc_parse_format Auto merge of #117819 - fmease:rustc_parse_format-stable-rustc, r=Nilstrieb 2023-11-15 14:55:35 +00:00
rustc_passes Replace custom_encodable with encodable. 2023-11-22 18:37:14 +11:00
rustc_privacy Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
rustc_query_impl Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
rustc_query_system Replace custom_encodable with encodable. 2023-11-22 18:37:14 +11:00
rustc_resolve Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
rustc_serialize Bump cfg(bootstrap)s 2023-11-15 19:41:28 -05:00
rustc_session Auto merge of #118071 - Urgau:check-cfg-cargo-feature, r=petrochenkov 2023-11-22 07:31:13 +00:00
rustc_smir Auto merge of #118152 - matthiaskrgr:rollup-bqcck4w, r=matthiaskrgr 2023-11-22 00:30:56 +00:00
rustc_span Replace no_ord_impl with orderable. 2023-11-22 18:38:17 +11:00
rustc_symbol_mangling Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
rustc_target Replace no_ord_impl with orderable. 2023-11-22 18:38:17 +11:00
rustc_trait_selection Replace no_ord_impl with orderable. 2023-11-22 18:38:17 +11:00
rustc_traits Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
rustc_transmute Update itertools to 0.11. 2023-11-22 08:13:21 +11:00
rustc_ty_utils Auto merge of #117928 - nnethercote:rustc_ast_pretty, r=fee1-dead 2023-11-22 05:09:33 +00:00
rustc_type_ir Replace no_ord_impl with orderable. 2023-11-22 18:38:17 +11:00
stable_mir Auto merge of #118152 - matthiaskrgr:rollup-bqcck4w, r=matthiaskrgr 2023-11-22 00:30:56 +00:00