Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
726 changes: 412 additions & 314 deletions compiler/rustc_lint/src/unused.rs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
#![warn(rustdoc::unescaped_backticks)]
#![deny(ffi_unwind_calls)]
#![warn(unreachable_pub)]
#![expect(unmustuse_in_always_ok)]
//
// Library features:
// tidy-alphabetical-start
Expand Down
1 change: 1 addition & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
#![allow(internal_features)]
#![deny(ffi_unwind_calls)]
#![warn(unreachable_pub)]
#![expect(unmustuse_in_always_ok)]
// Do not check link redundancy on bootstrapping phase
#![allow(rustdoc::redundant_explicit_links)]
#![warn(rustdoc::unescaped_backticks)]
Expand Down
1 change: 1 addition & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@
#![deny(unsafe_op_in_unsafe_fn)]
#![allow(rustdoc::redundant_explicit_links)]
#![warn(rustdoc::unescaped_backticks)]
#![expect(unmustuse_in_always_ok)]
// Ensure that std can be linked against panic_abort despite compiled with `-C panic=unwind`
#![deny(ffi_unwind_calls)]
// std may use features in a platform-specific way
Expand Down
4 changes: 4 additions & 0 deletions src/bootstrap/src/core/builder/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,10 @@ impl Builder<'_> {
// Lints just for `compiler/` crates.
if mode == Mode::Rustc {
lint_flags.push("-Wrustc::internal");
if compiler.stage > 0 {
lint_flags.push("-Aunmustuse_in_always_ok");
lint_flags.push("-Amustuse_in_always_ok");
}
lint_flags.push("-Drustc::symbol_intern_string_literal");
// FIXME(edition_2024): Change this to `-Wrust_2024_idioms` when all
// of the individual lints are satisfied.
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/binding/empty-types-in-patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#![allow(unreachable_patterns)]
#![allow(unreachable_code)]
#![allow(unused_variables)]
#![expect(unused_variables, unmustuse_in_always_ok)]

#[allow(dead_code)]
fn foo(z: !) {
Expand Down
17 changes: 16 additions & 1 deletion tests/ui/lint/unused/must_use-result-unit-uninhabited.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@

#![deny(unused_must_use)]
#![feature(never_type)]

#![expect(unmustuse_in_always_ok, mustuse_in_always_ok)]
use core::ops::{ControlFlow, ControlFlow::Continue};
use dep::{MyUninhabited, MyUninhabitedNonexhaustive};

#[must_use]
struct MustUse;

struct Struct;

fn result_unit_unit() -> Result<(), ()> {
Ok(())
}
Expand All @@ -19,6 +24,14 @@ fn result_unit_never() -> Result<(), !> {
Ok(())
}

fn result_struct_never() -> Result<Struct, !> {
Ok(Struct)
}

fn result_must_use_never() -> Result<MustUse, !> {
Ok(MustUse)
}

fn result_unit_myuninhabited() -> Result<(), MyUninhabited> {
Ok(())
}
Expand Down Expand Up @@ -80,6 +93,8 @@ fn main() {
result_unit_unit(); //~ ERROR: unused `Result` that must be used
result_unit_infallible();
result_unit_never();
result_must_use_never(); //~ ERROR: unused `MustUse` in a `Result` with an uninhabited error that must be used
result_struct_never();
result_unit_myuninhabited();
result_unit_myuninhabited_nonexhaustive(); //~ ERROR: unused `Result` that must be used
result_unit_assoctype(S1);
Expand Down
20 changes: 13 additions & 7 deletions tests/ui/lint/unused/must_use-result-unit-uninhabited.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: unused `Result` that must be used
--> $DIR/must_use-result-unit-uninhabited.rs:80:5
--> $DIR/must_use-result-unit-uninhabited.rs:93:5
|
LL | result_unit_unit();
| ^^^^^^^^^^^^^^^^^^
Expand All @@ -15,8 +15,14 @@ help: use `let _ = ...` to ignore the resulting value
LL | let _ = result_unit_unit();
| +++++++

error: unused `MustUse` in a `Result` with an uninhabited error that must be used
--> $DIR/must_use-result-unit-uninhabited.rs:96:5
|
LL | result_must_use_never();
| ^^^^^^^^^^^^^^^^^^^^^^^

error: unused `Result` that must be used
--> $DIR/must_use-result-unit-uninhabited.rs:84:5
--> $DIR/must_use-result-unit-uninhabited.rs:99:5
|
LL | result_unit_myuninhabited_nonexhaustive();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -28,7 +34,7 @@ LL | let _ = result_unit_myuninhabited_nonexhaustive();
| +++++++

error: unused `Result` that must be used
--> $DIR/must_use-result-unit-uninhabited.rs:86:5
--> $DIR/must_use-result-unit-uninhabited.rs:101:5
|
LL | result_unit_assoctype(S2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -40,7 +46,7 @@ LL | let _ = result_unit_assoctype(S2);
| +++++++

error: unused `Result` that must be used
--> $DIR/must_use-result-unit-uninhabited.rs:88:5
--> $DIR/must_use-result-unit-uninhabited.rs:103:5
|
LL | S2.method_use_assoc_type();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -52,7 +58,7 @@ LL | let _ = S2.method_use_assoc_type();
| +++++++

error: unused `ControlFlow` that must be used
--> $DIR/must_use-result-unit-uninhabited.rs:90:5
--> $DIR/must_use-result-unit-uninhabited.rs:105:5
|
LL | controlflow_unit();
| ^^^^^^^^^^^^^^^^^^
Expand All @@ -63,7 +69,7 @@ LL | let _ = controlflow_unit();
| +++++++

error: unused `Result` that must be used
--> $DIR/must_use-result-unit-uninhabited.rs:99:9
--> $DIR/must_use-result-unit-uninhabited.rs:114:9
|
LL | self.generate();
| ^^^^^^^^^^^^^^^
Expand All @@ -74,5 +80,5 @@ help: use `let _ = ...` to ignore the resulting value
LL | let _ = self.generate();
| +++++++

error: aborting due to 6 previous errors
error: aborting due to 7 previous errors

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//@ edition: 2015
//@ check-pass
#![warn(redundant_imports)]

#![expect(unmustuse_in_always_ok)]

use std::option::Option::Some;//~ WARNING the item `Some` is imported redundantly
use std::option::Option::None; //~ WARNING the item `None` is imported redundantly
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//@ check-pass
//@ edition:2021
#![warn(redundant_imports)]

#![expect(unmustuse_in_always_ok)]
use std::convert::TryFrom;//~ WARNING the item `TryFrom` is imported redundantly
use std::convert::TryInto;//~ WARNING the item `TryInto` is imported redundantly

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/never_type/never-result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#![allow(unreachable_patterns)]
// Test that we can extract a ! through pattern matching then use it as several different types.
#![feature(never_type)]

#![expect(unmustuse_in_always_ok)]
fn main() {
let x: Result<u32, !> = Ok(123);
match x {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/never_type/never-type-rvalues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![allow(dead_code)]
#![allow(path_statements)]
#![allow(unreachable_patterns)]

#![expect(unmustuse_in_always_ok)]
fn never_direct(x: !) {
x;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/never_type/question_mark_from_never.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//
//@ revisions: unit never
//@ check-pass
#![allow(internal_features)]
#![expect(internal_features, unmustuse_in_always_ok)]
#![feature(rustc_attrs, never_type)]
#![cfg_attr(unit, rustc_never_type_options(fallback = "unit"))]
#![cfg_attr(never, rustc_never_type_options(fallback = "never"))]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/never_type/try_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// over `TryFrom` being blanket impl for all `T: From`

#![feature(never_type)]

#![expect(unmustuse_in_always_ok)]
use std::convert::{TryInto, Infallible};

struct Foo<T> {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/pattern/usefulness/explain-unreachable-pats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![feature(exhaustive_patterns)]
#![deny(unreachable_patterns)]
//~^ NOTE lint level is defined here

#![expect(unmustuse_in_always_ok)]
#[rustfmt::skip]
fn main() {
match (0u8,) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//@ run-rustfix
#![feature(never_patterns)]
#![feature(exhaustive_patterns)]
#![allow(incomplete_features)]
#![expect(incomplete_features, unmustuse_in_always_ok)]
#![deny(unreachable_patterns)]

enum Void {}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/pattern/usefulness/rustfix-unreachable-pattern.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//@ run-rustfix
#![feature(never_patterns)]
#![feature(exhaustive_patterns)]
#![allow(incomplete_features)]
#![expect(incomplete_features, unmustuse_in_always_ok)]
#![deny(unreachable_patterns)]

enum Void {}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/print_type_sizes/uninhabited.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//@ ignore-pass
// ^-- needed because `--pass check` does not emit the output needed.
// FIXME: consider using an attribute instead of side-effects.

#![expect(unmustuse_in_always_ok)]
#![feature(never_type)]

pub fn test() {
Expand Down
1 change: 1 addition & 0 deletions tests/ui/reachable/unreachable-try-pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![feature(never_type, exhaustive_patterns)]
#![warn(unreachable_code)]
#![warn(unreachable_patterns)]
#![expect(unmustuse_in_always_ok)]

enum Void {}

Expand Down
6 changes: 3 additions & 3 deletions tests/ui/reachable/unreachable-try-pattern.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
warning: unreachable call
--> $DIR/unreachable-try-pattern.rs:19:33
--> $DIR/unreachable-try-pattern.rs:20:33
|
LL | let y = (match x { Ok(n) => Ok(n as u32), Err(e) => Err(e) })?;
| ^^ - any code following this expression is unreachable
Expand All @@ -13,7 +13,7 @@ LL | #![warn(unreachable_code)]
| ^^^^^^^^^^^^^^^^

warning: unreachable pattern
--> $DIR/unreachable-try-pattern.rs:19:24
--> $DIR/unreachable-try-pattern.rs:20:24
|
LL | let y = (match x { Ok(n) => Ok(n as u32), Err(e) => Err(e) })?;
| ^^^^^-----------------
Expand All @@ -29,7 +29,7 @@ LL | #![warn(unreachable_patterns)]
| ^^^^^^^^^^^^^^^^^^^^

warning: unreachable pattern
--> $DIR/unreachable-try-pattern.rs:30:40
--> $DIR/unreachable-try-pattern.rs:31:40
|
LL | let y = (match x { Ok(n) => Ok(n), Err(e) => Err(e) })?;
| ^^^^^^----------
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/rfcs/rfc-0000-never_patterns/unreachable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![feature(exhaustive_patterns)]
#![feature(never_patterns)]
#![allow(incomplete_features)]
#![expect(incomplete_features, unmustuse_in_always_ok)]
#![allow(dead_code, unreachable_code)]
#![deny(unreachable_patterns)]

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/rfcs/rfc-0000-never_patterns/use-bindings.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ check-pass
#![feature(never_patterns)]
#![allow(incomplete_features)]
#![expect(incomplete_features, unmustuse_in_always_ok)]

#[derive(Copy, Clone)]
enum Void {}
Expand Down
1 change: 1 addition & 0 deletions tests/ui/traits/const-traits/const-drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#![feature(const_trait_impl, const_destruct)]
#![feature(never_type)]
#![cfg_attr(precise, feature(const_precise_live_drops))]
#![expect(unmustuse_in_always_ok)]

use std::marker::Destruct;

Expand Down
Loading