2020-10-21 12:47:34 +00:00
|
|
|
#![allow(clippy::redundant_clone)]
|
|
|
|
#![feature(custom_inner_attributes)]
|
|
|
|
#![clippy::msrv = "1.0.0"]
|
|
|
|
|
2020-12-05 12:59:22 +00:00
|
|
|
use std::ops::{Deref, RangeFrom};
|
2020-10-21 12:47:34 +00:00
|
|
|
|
2021-09-03 06:34:34 +00:00
|
|
|
fn approx_const() {
|
|
|
|
let log2_10 = 3.321928094887362;
|
|
|
|
let log10_2 = 0.301029995663981;
|
|
|
|
}
|
|
|
|
|
2021-04-26 16:09:24 +00:00
|
|
|
fn cloned_instead_of_copied() {
|
|
|
|
let _ = [1].iter().cloned();
|
|
|
|
}
|
|
|
|
|
2020-10-21 12:47:34 +00:00
|
|
|
fn option_as_ref_deref() {
|
|
|
|
let mut opt = Some(String::from("123"));
|
|
|
|
|
|
|
|
let _ = opt.as_ref().map(String::as_str);
|
|
|
|
let _ = opt.as_ref().map(|x| x.as_str());
|
|
|
|
let _ = opt.as_mut().map(String::as_mut_str);
|
|
|
|
let _ = opt.as_mut().map(|x| x.as_mut_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn match_like_matches() {
|
|
|
|
let _y = match Some(5) {
|
|
|
|
Some(0) => true,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn match_same_arms() {
|
|
|
|
match (1, 2, 3) {
|
|
|
|
(1, .., 3) => 42,
|
|
|
|
(.., 3) => 42, //~ ERROR match arms have same body
|
|
|
|
_ => 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn match_same_arms2() {
|
|
|
|
let _ = match Some(42) {
|
|
|
|
Some(_) => 24,
|
|
|
|
None => 24, //~ ERROR match arms have same body
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-11-29 11:38:56 +00:00
|
|
|
pub fn manual_strip_msrv() {
|
2020-10-21 12:47:34 +00:00
|
|
|
let s = "hello, world!";
|
|
|
|
if s.starts_with("hello, ") {
|
|
|
|
assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-05 12:59:22 +00:00
|
|
|
pub fn redundant_fieldnames() {
|
|
|
|
let start = 0;
|
|
|
|
let _ = RangeFrom { start: start };
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn redundant_static_lifetime() {
|
|
|
|
const VAR_ONE: &'static str = "Test constant #1";
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn checked_conversion() {
|
|
|
|
let value: i64 = 42;
|
|
|
|
let _ = value <= (u32::max_value() as i64) && value >= 0;
|
|
|
|
let _ = value <= (u32::MAX as i64) && value >= 0;
|
|
|
|
}
|
|
|
|
|
2020-12-20 12:00:17 +00:00
|
|
|
pub struct FromOverInto(String);
|
|
|
|
|
|
|
|
impl Into<FromOverInto> for String {
|
|
|
|
fn into(self) -> FromOverInto {
|
|
|
|
FromOverInto(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-05 12:59:22 +00:00
|
|
|
pub fn filter_map_next() {
|
|
|
|
let a = ["1", "lol", "3", "NaN", "5"];
|
|
|
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let _: Option<u32> = vec![1, 2, 3, 4, 5, 6]
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|x| {
|
|
|
|
if x == 2 {
|
|
|
|
Some(x * 2)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.next();
|
|
|
|
}
|
|
|
|
|
2020-12-09 11:09:33 +00:00
|
|
|
#[allow(clippy::no_effect)]
|
|
|
|
#[allow(clippy::short_circuit_statement)]
|
|
|
|
#[allow(clippy::unnecessary_operation)]
|
2020-12-05 12:59:22 +00:00
|
|
|
pub fn manual_range_contains() {
|
2020-12-09 11:09:33 +00:00
|
|
|
let x = 5;
|
2020-12-05 12:59:22 +00:00
|
|
|
x >= 8 && x < 12;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn use_self() {
|
2022-03-27 12:41:09 +00:00
|
|
|
struct Foo;
|
2020-12-05 12:59:22 +00:00
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn new() -> Foo {
|
|
|
|
Foo {}
|
|
|
|
}
|
|
|
|
fn test() -> Foo {
|
|
|
|
Foo::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn replace_with_default() {
|
|
|
|
let mut s = String::from("foo");
|
2020-12-09 11:09:33 +00:00
|
|
|
let _ = std::mem::replace(&mut s, String::default());
|
2020-12-05 12:59:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn map_unwrap_or() {
|
|
|
|
let opt = Some(1);
|
|
|
|
|
|
|
|
// Check for `option.map(_).unwrap_or(_)` use.
|
|
|
|
// Single line case.
|
|
|
|
let _ = opt
|
|
|
|
.map(|x| x + 1)
|
|
|
|
// Should lint even though this call is on a separate line.
|
|
|
|
.unwrap_or(0);
|
|
|
|
}
|
|
|
|
|
2020-12-09 11:09:33 +00:00
|
|
|
// Could be const
|
|
|
|
fn missing_const_for_fn() -> i32 {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
|
2021-03-26 09:56:05 +00:00
|
|
|
fn unnest_or_patterns() {
|
|
|
|
struct TS(u8, u8);
|
|
|
|
if let TS(0, x) | TS(1, x) = TS(0, 0) {}
|
|
|
|
}
|
|
|
|
|
2021-11-07 18:52:34 +00:00
|
|
|
#[cfg_attr(rustfmt, rustfmt_skip)]
|
|
|
|
fn deprecated_cfg_attr() {}
|
|
|
|
|
2021-11-09 17:35:35 +00:00
|
|
|
#[warn(clippy::cast_lossless)]
|
|
|
|
fn int_from_bool() -> u8 {
|
|
|
|
true as u8
|
|
|
|
}
|
|
|
|
|
2022-03-29 17:19:16 +00:00
|
|
|
fn err_expect() {
|
|
|
|
let x: Result<u32, &str> = Ok(10);
|
|
|
|
x.err().expect("Testing expect_err");
|
|
|
|
}
|
|
|
|
|
2022-04-04 16:38:38 +00:00
|
|
|
fn cast_abs_to_unsigned() {
|
|
|
|
let x: i32 = 10;
|
|
|
|
assert_eq!(10u32, x.abs() as u32);
|
|
|
|
}
|
|
|
|
|
2022-06-21 18:13:15 +00:00
|
|
|
fn manual_rem_euclid() {
|
|
|
|
let x: i32 = 10;
|
|
|
|
let _: i32 = ((x % 4) + 4) % 4;
|
|
|
|
}
|
|
|
|
|
2020-10-21 12:47:34 +00:00
|
|
|
fn main() {
|
2020-12-05 12:59:22 +00:00
|
|
|
filter_map_next();
|
|
|
|
checked_conversion();
|
|
|
|
redundant_fieldnames();
|
|
|
|
redundant_static_lifetime();
|
2020-10-21 12:47:34 +00:00
|
|
|
option_as_ref_deref();
|
|
|
|
match_like_matches();
|
|
|
|
match_same_arms();
|
|
|
|
match_same_arms2();
|
|
|
|
manual_strip_msrv();
|
2020-12-05 12:59:22 +00:00
|
|
|
manual_range_contains();
|
|
|
|
use_self();
|
|
|
|
replace_with_default();
|
|
|
|
map_unwrap_or();
|
2020-12-09 11:09:33 +00:00
|
|
|
missing_const_for_fn();
|
2021-03-26 09:56:05 +00:00
|
|
|
unnest_or_patterns();
|
2021-11-09 17:35:35 +00:00
|
|
|
int_from_bool();
|
2022-03-29 17:19:16 +00:00
|
|
|
err_expect();
|
2022-04-04 16:38:38 +00:00
|
|
|
cast_abs_to_unsigned();
|
2022-06-21 18:13:15 +00:00
|
|
|
manual_rem_euclid();
|
2020-10-21 12:47:34 +00:00
|
|
|
}
|
2020-11-29 11:38:56 +00:00
|
|
|
|
2021-11-07 18:52:34 +00:00
|
|
|
mod just_under_msrv {
|
2020-11-29 11:38:56 +00:00
|
|
|
#![feature(custom_inner_attributes)]
|
2021-11-07 18:52:34 +00:00
|
|
|
#![clippy::msrv = "1.44.0"]
|
2020-11-29 11:38:56 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let s = "hello, world!";
|
|
|
|
if s.starts_with("hello, ") {
|
|
|
|
assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 18:52:34 +00:00
|
|
|
mod meets_msrv {
|
2020-11-29 11:38:56 +00:00
|
|
|
#![feature(custom_inner_attributes)]
|
2021-11-07 18:52:34 +00:00
|
|
|
#![clippy::msrv = "1.45.0"]
|
2020-11-29 11:38:56 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let s = "hello, world!";
|
|
|
|
if s.starts_with("hello, ") {
|
|
|
|
assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod just_above_msrv {
|
|
|
|
#![feature(custom_inner_attributes)]
|
2021-11-07 18:52:34 +00:00
|
|
|
#![clippy::msrv = "1.46.0"]
|
2020-11-29 11:38:56 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let s = "hello, world!";
|
|
|
|
if s.starts_with("hello, ") {
|
|
|
|
assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-23 20:34:52 +00:00
|
|
|
|
|
|
|
mod const_rem_euclid {
|
|
|
|
#![feature(custom_inner_attributes)]
|
|
|
|
#![clippy::msrv = "1.50.0"]
|
|
|
|
|
|
|
|
pub const fn const_rem_euclid_4(num: i32) -> i32 {
|
|
|
|
((num % 4) + 4) % 4
|
|
|
|
}
|
|
|
|
}
|