rust-clippy/tests/ui/no_effect.rs

198 lines
4.2 KiB
Rust
Raw Normal View History

#![feature(fn_traits, unboxed_closures)]
#![warn(clippy::no_effect_underscore_binding)]
#![allow(
clippy::deref_addrof,
clippy::redundant_field_names,
clippy::uninlined_format_args,
2023-06-06 20:56:57 +00:00
clippy::unnecessary_struct_initialization,
clippy::useless_vec
)]
use std::fmt::Display;
use std::ops::{Neg, Shl};
struct Cout;
impl<T> Shl<T> for Cout
where
T: Display,
{
type Output = Self;
fn shl(self, rhs: T) -> Self::Output {
println!("{}", rhs);
self
}
}
impl Neg for Cout {
type Output = Self;
fn neg(self) -> Self::Output {
println!("hello world");
self
}
}
2015-10-28 16:50:00 +00:00
struct Tuple(i32);
struct Struct {
2018-12-09 22:26:16 +00:00
field: i32,
2015-10-28 16:50:00 +00:00
}
enum Enum {
2016-02-01 11:51:33 +00:00
Tuple(i32),
Struct { field: i32 },
2015-10-28 16:50:00 +00:00
}
struct DropStruct {
2018-12-09 22:26:16 +00:00
field: i32,
}
impl Drop for DropStruct {
fn drop(&mut self) {}
}
struct DropTuple(i32);
impl Drop for DropTuple {
fn drop(&mut self) {}
}
enum DropEnum {
Tuple(i32),
Struct { field: i32 },
}
impl Drop for DropEnum {
fn drop(&mut self) {}
}
struct FooString {
s: String,
}
2016-08-28 17:43:55 +00:00
union Union {
a: u8,
b: f64,
}
2018-12-09 22:26:16 +00:00
fn get_number() -> i32 {
0
}
fn get_struct() -> Struct {
Struct { field: 0 }
}
fn get_drop_struct() -> DropStruct {
DropStruct { field: 0 }
}
2015-10-28 16:50:00 +00:00
2018-12-09 22:26:16 +00:00
unsafe fn unsafe_fn() -> i32 {
0
}
2016-05-25 16:51:35 +00:00
struct GreetStruct1;
impl FnOnce<(&str,)> for GreetStruct1 {
type Output = ();
extern "rust-call" fn call_once(self, (who,): (&str,)) -> Self::Output {
println!("hello {}", who);
}
}
struct GreetStruct2();
impl FnOnce<(&str,)> for GreetStruct2 {
type Output = ();
extern "rust-call" fn call_once(self, (who,): (&str,)) -> Self::Output {
println!("hello {}", who);
}
}
struct GreetStruct3;
impl FnOnce<(&str,)> for GreetStruct3 {
type Output = ();
extern "rust-call" fn call_once(self, (who,): (&str,)) -> Self::Output {
println!("hello {}", who);
}
}
2015-10-28 16:50:00 +00:00
fn main() {
let s = get_struct();
2017-02-08 13:58:07 +00:00
0;
//~^ ERROR: statement with no effect
2017-02-08 13:58:07 +00:00
Tuple(0);
//~^ ERROR: statement with no effect
2017-02-08 13:58:07 +00:00
Struct { field: 0 };
//~^ ERROR: statement with no effect
2017-02-08 13:58:07 +00:00
Struct { ..s };
//~^ ERROR: statement with no effect
2017-02-08 13:58:07 +00:00
Union { a: 0 };
//~^ ERROR: statement with no effect
2017-02-08 13:58:07 +00:00
Enum::Tuple(0);
//~^ ERROR: statement with no effect
2017-02-08 13:58:07 +00:00
Enum::Struct { field: 0 };
//~^ ERROR: statement with no effect
2017-02-08 13:58:07 +00:00
5 + 6;
//~^ ERROR: statement with no effect
2017-02-08 13:58:07 +00:00
*&42;
//~^ ERROR: statement with no effect
2017-02-08 13:58:07 +00:00
&6;
//~^ ERROR: statement with no effect
2017-02-08 13:58:07 +00:00
(5, 6, 7);
//~^ ERROR: statement with no effect
2017-02-08 13:58:07 +00:00
..;
//~^ ERROR: statement with no effect
2017-02-08 13:58:07 +00:00
5..;
//~^ ERROR: statement with no effect
2017-02-08 13:58:07 +00:00
..5;
//~^ ERROR: statement with no effect
2017-02-08 13:58:07 +00:00
5..6;
//~^ ERROR: statement with no effect
2017-09-28 17:40:19 +00:00
5..=6;
//~^ ERROR: statement with no effect
2017-02-08 13:58:07 +00:00
[42, 55];
//~^ ERROR: statement with no effect
2017-02-08 13:58:07 +00:00
[42, 55][1];
//~^ ERROR: statement with no effect
2017-02-08 13:58:07 +00:00
(42, 55).1;
//~^ ERROR: statement with no effect
2017-02-08 13:58:07 +00:00
[42; 55];
//~^ ERROR: statement with no effect
2017-02-08 13:58:07 +00:00
[42; 55][13];
//~^ ERROR: statement with no effect
2016-02-11 12:50:41 +00:00
let mut x = 0;
2017-02-08 13:58:07 +00:00
|| x += 5;
//~^ ERROR: statement with no effect
let s: String = "foo".into();
FooString { s: s };
//~^ ERROR: statement with no effect
let _unused = 1;
//~^ ERROR: binding to `_` prefixed variable with no side-effect
//~| NOTE: `-D clippy::no-effect-underscore-binding` implied by `-D warnings`
let _penguin = || println!("Some helpful closure");
//~^ ERROR: binding to `_` prefixed variable with no side-effect
let _duck = Struct { field: 0 };
//~^ ERROR: binding to `_` prefixed variable with no side-effect
let _cat = [2, 4, 6, 8][2];
//~^ ERROR: binding to `_` prefixed variable with no side-effect
let _issue_12166 = 42;
let underscore_variable_above_can_be_used_dont_lint = _issue_12166;
2015-10-28 16:50:00 +00:00
#[allow(clippy::no_effect)]
0;
2015-10-28 16:50:00 +00:00
// Do not warn
get_number();
2016-05-25 16:51:35 +00:00
unsafe { unsafe_fn() };
let _used = get_struct();
let _x = vec![1];
DropStruct { field: 0 };
DropTuple(0);
DropEnum::Tuple(0);
DropEnum::Struct { field: 0 };
GreetStruct1("world");
GreetStruct2()("world");
GreetStruct3 {}("world");
fn n() -> i32 {
42
}
Cout << 142;
-Cout;
2015-10-28 16:50:00 +00:00
}