2023-08-24 19:32:12 +00:00
|
|
|
//@aux-build:proc_macros.rs
|
2021-07-01 16:17:38 +00:00
|
|
|
|
2021-02-25 10:25:22 +00:00
|
|
|
#![warn(clippy::default_numeric_fallback)]
|
2022-05-05 14:12:52 +00:00
|
|
|
#![allow(
|
|
|
|
unused,
|
|
|
|
clippy::never_loop,
|
|
|
|
clippy::no_effect,
|
|
|
|
clippy::unnecessary_operation,
|
|
|
|
clippy::branches_sharing_code,
|
2023-03-10 09:53:50 +00:00
|
|
|
clippy::let_unit_value,
|
|
|
|
clippy::let_with_type_underscore
|
2022-05-05 14:12:52 +00:00
|
|
|
)]
|
2021-02-25 10:25:22 +00:00
|
|
|
|
2023-03-24 13:04:35 +00:00
|
|
|
extern crate proc_macros;
|
|
|
|
use proc_macros::{external, inline_macros};
|
2021-07-01 16:17:38 +00:00
|
|
|
|
2021-02-25 10:25:22 +00:00
|
|
|
mod basic_expr {
|
|
|
|
fn test() {
|
|
|
|
// Should lint unsuffixed literals typed `i32`.
|
2021-07-15 08:44:10 +00:00
|
|
|
let x = 22_i32;
|
|
|
|
let x = [1_i32, 2_i32, 3_i32];
|
|
|
|
let x = if true { (1_i32, 2_i32) } else { (3_i32, 4_i32) };
|
|
|
|
let x = match 1_i32 {
|
|
|
|
1_i32 => 1_i32,
|
|
|
|
_ => 2_i32,
|
2021-02-25 10:25:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Should NOT lint suffixed literals.
|
|
|
|
let x = 22_i32;
|
|
|
|
|
|
|
|
// Should NOT lint literals in init expr if `Local` has a type annotation.
|
|
|
|
let x: [i32; 3] = [1, 2, 3];
|
|
|
|
let x: (i32, i32) = if true { (1, 2) } else { (3, 4) };
|
|
|
|
let x: _ = 1;
|
2022-10-23 13:18:45 +00:00
|
|
|
let x: u64 = 1;
|
|
|
|
const CONST_X: i8 = 1;
|
2021-02-25 10:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod nested_local {
|
|
|
|
fn test() {
|
|
|
|
let x: _ = {
|
|
|
|
// Should lint this because this literal is not bound to any types.
|
2021-07-15 08:44:10 +00:00
|
|
|
let y = 1_i32;
|
2021-02-25 10:25:22 +00:00
|
|
|
|
|
|
|
// Should NOT lint this because this literal is bound to `_` of outer `Local`.
|
|
|
|
1
|
|
|
|
};
|
|
|
|
|
|
|
|
let x: _ = if true {
|
|
|
|
// Should lint this because this literal is not bound to any types.
|
2021-07-15 08:44:10 +00:00
|
|
|
let y = 1_i32;
|
2021-02-25 10:25:22 +00:00
|
|
|
|
|
|
|
// Should NOT lint this because this literal is bound to `_` of outer `Local`.
|
|
|
|
1
|
|
|
|
} else {
|
|
|
|
// Should lint this because this literal is not bound to any types.
|
2021-07-15 08:44:10 +00:00
|
|
|
let y = 1_i32;
|
2021-02-25 10:25:22 +00:00
|
|
|
|
|
|
|
// Should NOT lint this because this literal is bound to `_` of outer `Local`.
|
|
|
|
2
|
|
|
|
};
|
2022-10-23 13:18:45 +00:00
|
|
|
|
|
|
|
const CONST_X: i32 = {
|
|
|
|
// Should lint this because this literal is not bound to any types.
|
|
|
|
let y = 1_i32;
|
|
|
|
|
|
|
|
// Should NOT lint this because this literal is bound to `_` of outer `Local`.
|
|
|
|
1
|
|
|
|
};
|
2021-02-25 10:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod function_def {
|
|
|
|
fn ret_i32() -> i32 {
|
2024-01-11 16:27:03 +00:00
|
|
|
1
|
2021-02-25 10:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test() {
|
|
|
|
// Should lint this because return type is inferred to `i32` and NOT bound to a concrete
|
|
|
|
// type.
|
2021-07-15 08:44:10 +00:00
|
|
|
let f = || -> _ { 1_i32 };
|
2021-02-25 10:25:22 +00:00
|
|
|
|
|
|
|
// Even though the output type is specified,
|
|
|
|
// this unsuffixed literal is linted to reduce heuristics and keep codebase simple.
|
2021-07-15 08:44:10 +00:00
|
|
|
let f = || -> i32 { 1_i32 };
|
2021-02-25 10:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod function_calls {
|
|
|
|
fn concrete_arg(x: i32) {}
|
|
|
|
|
|
|
|
fn generic_arg<T>(t: T) {}
|
|
|
|
|
|
|
|
fn test() {
|
|
|
|
// Should NOT lint this because the argument type is bound to a concrete type.
|
|
|
|
concrete_arg(1);
|
|
|
|
|
|
|
|
// Should lint this because the argument type is inferred to `i32` and NOT bound to a concrete type.
|
2021-07-15 08:44:10 +00:00
|
|
|
generic_arg(1_i32);
|
2021-02-25 10:25:22 +00:00
|
|
|
|
|
|
|
// Should lint this because the argument type is inferred to `i32` and NOT bound to a concrete type.
|
2021-07-15 08:44:10 +00:00
|
|
|
let x: _ = generic_arg(1_i32);
|
2021-02-25 10:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod struct_ctor {
|
|
|
|
struct ConcreteStruct {
|
|
|
|
x: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct GenericStruct<T> {
|
|
|
|
x: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test() {
|
|
|
|
// Should NOT lint this because the field type is bound to a concrete type.
|
|
|
|
ConcreteStruct { x: 1 };
|
|
|
|
|
|
|
|
// Should lint this because the field type is inferred to `i32` and NOT bound to a concrete type.
|
2021-07-15 08:44:10 +00:00
|
|
|
GenericStruct { x: 1_i32 };
|
|
|
|
|
|
|
|
// Should lint this because the field type is inferred to `i32` and NOT bound to a concrete type.
|
|
|
|
let _ = GenericStruct { x: 1_i32 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod enum_ctor {
|
|
|
|
enum ConcreteEnum {
|
|
|
|
X(i32),
|
|
|
|
}
|
|
|
|
|
|
|
|
enum GenericEnum<T> {
|
|
|
|
X(T),
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test() {
|
|
|
|
// Should NOT lint this because the field type is bound to a concrete type.
|
|
|
|
ConcreteEnum::X(1);
|
2021-02-25 10:25:22 +00:00
|
|
|
|
|
|
|
// Should lint this because the field type is inferred to `i32` and NOT bound to a concrete type.
|
2021-07-15 08:44:10 +00:00
|
|
|
GenericEnum::X(1_i32);
|
2021-02-25 10:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod method_calls {
|
2022-04-07 17:39:59 +00:00
|
|
|
struct StructForMethodCallTest;
|
2021-02-25 10:25:22 +00:00
|
|
|
|
|
|
|
impl StructForMethodCallTest {
|
|
|
|
fn concrete_arg(&self, x: i32) {}
|
|
|
|
|
|
|
|
fn generic_arg<T>(&self, t: T) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test() {
|
|
|
|
let s = StructForMethodCallTest {};
|
|
|
|
|
|
|
|
// Should NOT lint this because the argument type is bound to a concrete type.
|
|
|
|
s.concrete_arg(1);
|
|
|
|
|
|
|
|
// Should lint this because the argument type is bound to a concrete type.
|
2021-07-15 08:44:10 +00:00
|
|
|
s.generic_arg(1_i32);
|
2021-02-25 10:25:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-01 16:17:38 +00:00
|
|
|
mod in_macro {
|
2023-03-24 13:04:35 +00:00
|
|
|
use super::*;
|
2021-07-01 16:17:38 +00:00
|
|
|
|
|
|
|
// Should lint in internal macro.
|
2023-03-24 13:04:35 +00:00
|
|
|
#[inline_macros]
|
2021-07-01 16:17:38 +00:00
|
|
|
fn internal() {
|
2023-03-24 13:04:35 +00:00
|
|
|
inline!(let x = 22_i32;);
|
2021-07-01 16:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Should NOT lint in external macro.
|
|
|
|
fn external() {
|
2023-03-24 13:04:35 +00:00
|
|
|
external!(let x = 22;);
|
2021-07-01 16:17:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-16 15:39:06 +00:00
|
|
|
fn check_expect_suppression() {
|
|
|
|
#[expect(clippy::default_numeric_fallback)]
|
|
|
|
let x = 21;
|
|
|
|
}
|
|
|
|
|
2024-01-11 16:27:03 +00:00
|
|
|
mod type_already_inferred {
|
|
|
|
// Should NOT lint if bound to return type
|
|
|
|
fn ret_i32() -> i32 {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should NOT lint if bound to return type
|
|
|
|
fn ret_if_i32(b: bool) -> i32 {
|
|
|
|
if b { 100 } else { 0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should NOT lint if bound to return type
|
|
|
|
fn ret_i32_tuple() -> (i32, i32) {
|
|
|
|
(0, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should NOT lint if bound to return type
|
|
|
|
fn ret_stmt(b: bool) -> (i32, i32) {
|
|
|
|
if b {
|
|
|
|
return (0, 1);
|
|
|
|
}
|
|
|
|
(0, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::useless_vec)]
|
|
|
|
fn vec_macro() {
|
|
|
|
// Should NOT lint in `vec!` call if the type was already stated
|
|
|
|
let data_i32: Vec<i32> = vec![1, 2, 3];
|
|
|
|
let data_i32 = vec![1_i32, 2_i32, 3_i32];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-25 18:17:36 +00:00
|
|
|
mod issue12159 {
|
|
|
|
#![allow(non_upper_case_globals, clippy::exhaustive_structs)]
|
|
|
|
pub struct Foo;
|
|
|
|
|
|
|
|
static F: i32 = 1;
|
|
|
|
impl Foo {
|
|
|
|
const LIFE_u8: u8 = 42;
|
|
|
|
const LIFE_i8: i8 = 42;
|
|
|
|
const LIFE_u16: u16 = 42;
|
|
|
|
const LIFE_i16: i16 = 42;
|
|
|
|
const LIFE_u32: u32 = 42;
|
|
|
|
const LIFE_i32: i32 = 42;
|
|
|
|
const LIFE_u64: u64 = 42;
|
|
|
|
const LIFE_i64: i64 = 42;
|
|
|
|
const LIFE_u128: u128 = 42;
|
|
|
|
const LIFE_i128: i128 = 42;
|
|
|
|
const LIFE_usize: usize = 42;
|
|
|
|
const LIFE_isize: isize = 42;
|
|
|
|
const LIFE_f32: f32 = 42.;
|
|
|
|
const LIFE_f64: f64 = 42.;
|
|
|
|
|
|
|
|
const fn consts() {
|
|
|
|
const LIFE_u8: u8 = 42;
|
|
|
|
const LIFE_i8: i8 = 42;
|
|
|
|
const LIFE_u16: u16 = 42;
|
|
|
|
const LIFE_i16: i16 = 42;
|
|
|
|
const LIFE_u32: u32 = 42;
|
|
|
|
const LIFE_i32: i32 = 42;
|
|
|
|
const LIFE_u64: u64 = 42;
|
|
|
|
const LIFE_i64: i64 = 42;
|
|
|
|
const LIFE_u128: u128 = 42;
|
|
|
|
const LIFE_i128: i128 = 42;
|
|
|
|
const LIFE_usize: usize = 42;
|
|
|
|
const LIFE_isize: isize = 42;
|
|
|
|
const LIFE_f32: f32 = 42.;
|
|
|
|
const LIFE_f64: f64 = 42.;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 10:25:22 +00:00
|
|
|
fn main() {}
|