forked from rust-lang/rust-clippy
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new lint [
unconstrained_numeric_literal
]
- Loading branch information
Showing
7 changed files
with
501 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
clippy_lints/src/guidelines/unconstrained_numeric_literal.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use clippy_utils::is_lint_allowed; | ||
use clippy_utils::source::snippet_opt; | ||
use rustc_errors::{Applicability, MultiSpan}; | ||
use rustc_hir::intravisit::{walk_expr, Visitor}; | ||
use rustc_hir::{Expr, ExprKind, Local, TyKind}; | ||
use rustc_lint::{LateContext, LintContext}; | ||
use rustc_middle::lint::in_external_macro; | ||
use rustc_span::Span; | ||
|
||
use super::UNCONSTRAINED_NUMERIC_LITERAL; | ||
|
||
pub(super) fn check_local<'tcx>(cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) { | ||
if !is_lint_allowed(cx, UNCONSTRAINED_NUMERIC_LITERAL, local.hir_id) | ||
&& let Some(init) = local.init | ||
&& !in_external_macro(cx.sess(), init.span) | ||
&& local_has_implicit_ty(local) | ||
{ | ||
let mut visitor = LitVisitor::new(); | ||
visitor.visit_expr(init); | ||
|
||
// The type could be wildcard (`_`), therefore we need to include its span for suggestion. | ||
let span = if let Some(ty) = local.ty { | ||
local.pat.span.to(ty.span) | ||
} else { | ||
local.pat.span | ||
}; | ||
|
||
if !visitor.unconstrained_lit_spans.is_empty() { | ||
span_lint_and_then( | ||
cx, | ||
UNCONSTRAINED_NUMERIC_LITERAL, | ||
span, | ||
"type of this numeric variable is unconstrained", | ||
|diag| { | ||
let sugg = format!( | ||
"{}: {}", | ||
snippet_opt(cx, local.pat.span).unwrap_or("_".to_string()), | ||
ty_suggestion(cx, init), | ||
); | ||
diag.span_suggestion( | ||
span, | ||
"either add suffix to above numeric literal(s) or label the type explicitly", | ||
sugg, | ||
Applicability::MachineApplicable | ||
); | ||
diag.span_note( | ||
MultiSpan::from_spans(visitor.unconstrained_lit_spans), | ||
"unconstrained numeric literals happened here", | ||
); | ||
} | ||
); | ||
} | ||
} | ||
} | ||
|
||
fn local_has_implicit_ty(local: &Local<'_>) -> bool { | ||
match local.ty { | ||
Some(ty) if matches!(ty.kind, TyKind::Infer) => true, | ||
None => true, | ||
_ => false, | ||
} | ||
} | ||
|
||
struct LitVisitor { | ||
unconstrained_lit_spans: Vec<Span>, | ||
} | ||
|
||
impl LitVisitor { | ||
fn new() -> Self { | ||
Self { | ||
unconstrained_lit_spans: vec![], | ||
} | ||
} | ||
} | ||
|
||
impl<'hir> Visitor<'hir> for LitVisitor { | ||
fn visit_expr(&mut self, ex: &'hir Expr<'hir>) { | ||
match &ex.kind { | ||
// These are fine, because the numerics in them are always inferred. | ||
ExprKind::Call(..) | ExprKind::MethodCall(..) => (), | ||
ExprKind::Lit(lit) => { | ||
if lit.node.is_numeric() && lit.node.is_unsuffixed() { | ||
self.unconstrained_lit_spans.push(lit.span); | ||
} | ||
}, | ||
ExprKind::Closure(_) => { | ||
println!("span of closure: {:?}", ex.span); | ||
walk_expr(self, ex); | ||
}, | ||
_ => walk_expr(self, ex), | ||
} | ||
} | ||
|
||
// Don't visit local in this visitor, `Local`s are handled in `check_local` call. | ||
fn visit_local(&mut self, _: &'hir Local<'hir>) {} | ||
} | ||
|
||
fn ty_suggestion(cx: &LateContext<'_>, init: &Expr<'_>) -> String { | ||
let ty = cx.typeck_results().expr_ty(init); | ||
ty.to_string() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
//@aux-build:../auxiliary/proc_macros.rs | ||
|
||
#![feature(lint_reasons)] | ||
#![warn(clippy::unconstrained_numeric_literal)] | ||
#![allow(clippy::let_with_type_underscore, clippy::let_and_return)] | ||
|
||
extern crate proc_macros; | ||
use proc_macros::{external, inline_macros}; | ||
|
||
mod basic_expr { | ||
fn test() { | ||
let x: i32 = 22; | ||
//~^ ERROR: type of this numeric variable is unconstrained | ||
//~| NOTE: `-D clippy::unconstrained-numeric-literal` implied by `-D warnings` | ||
let x: f64 = 22.0; | ||
//~^ ERROR: type of this numeric variable is unconstrained | ||
let x: [i32; 3] = [1, 2, 3]; | ||
//~^ ERROR: type of this numeric variable is unconstrained | ||
let x: (i32, i32) = if true { (1, 2) } else { (3, 4) }; | ||
//~^ ERROR: type of this numeric variable is unconstrained | ||
let x: (f64, i32, f64) = if true { (1.0, 2, 3.0) } else { (3.0, 4, 5.0) }; | ||
//~^ ERROR: type of this numeric variable is unconstrained | ||
let x: i32 = match 1 { | ||
//~^ ERROR: type of this numeric variable is unconstrained | ||
1 => 1, | ||
_ => 2, | ||
}; | ||
// Has type annotation but it's a wildcard. | ||
let x: i32 = 1; | ||
//~^ ERROR: type of this numeric variable is unconstrained | ||
|
||
let x = 22_i32; | ||
let x: [i32; 3] = [1, 2, 3]; | ||
let x: (i32, i32) = if true { (1, 2) } else { (3, 4) }; | ||
let x: u64 = 1; | ||
const CONST_X: i8 = 1; | ||
} | ||
} | ||
|
||
mod nested_local { | ||
fn test() { | ||
let x: i32 = { | ||
//~^ ERROR: type of this numeric variable is unconstrained | ||
let y: i32 = 1; | ||
//~^ ERROR: type of this numeric variable is unconstrained | ||
1 | ||
}; | ||
|
||
let x: i32 = { | ||
let y: i32 = 1; | ||
//~^ ERROR: type of this numeric variable is unconstrained | ||
1 | ||
}; | ||
|
||
const CONST_X: i32 = { | ||
let y: i32 = 1; | ||
//~^ ERROR: type of this numeric variable is unconstrained | ||
1 | ||
}; | ||
} | ||
} | ||
|
||
mod in_macro { | ||
use super::*; | ||
|
||
#[inline_macros] | ||
fn internal() { | ||
inline!(let x: i32 = 22;); | ||
//~^ ERROR: type of this numeric variable is unconstrained | ||
} | ||
|
||
fn external() { | ||
external!(let x = 22;); | ||
} | ||
} | ||
|
||
fn check_expect_suppression() { | ||
#[expect(clippy::unconstrained_numeric_literal)] | ||
let x = 21; | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
//@aux-build:../auxiliary/proc_macros.rs | ||
|
||
#![feature(lint_reasons)] | ||
#![warn(clippy::unconstrained_numeric_literal)] | ||
#![allow(clippy::let_with_type_underscore, clippy::let_and_return)] | ||
|
||
extern crate proc_macros; | ||
use proc_macros::{external, inline_macros}; | ||
|
||
mod basic_expr { | ||
fn test() { | ||
let x = 22; | ||
//~^ ERROR: type of this numeric variable is unconstrained | ||
//~| NOTE: `-D clippy::unconstrained-numeric-literal` implied by `-D warnings` | ||
let x = 22.0; | ||
//~^ ERROR: type of this numeric variable is unconstrained | ||
let x = [1, 2, 3]; | ||
//~^ ERROR: type of this numeric variable is unconstrained | ||
let x = if true { (1, 2) } else { (3, 4) }; | ||
//~^ ERROR: type of this numeric variable is unconstrained | ||
let x = if true { (1.0, 2, 3.0) } else { (3.0, 4, 5.0) }; | ||
//~^ ERROR: type of this numeric variable is unconstrained | ||
let x = match 1 { | ||
//~^ ERROR: type of this numeric variable is unconstrained | ||
1 => 1, | ||
_ => 2, | ||
}; | ||
// Has type annotation but it's a wildcard. | ||
let x: _ = 1; | ||
//~^ ERROR: type of this numeric variable is unconstrained | ||
|
||
let x = 22_i32; | ||
let x: [i32; 3] = [1, 2, 3]; | ||
let x: (i32, i32) = if true { (1, 2) } else { (3, 4) }; | ||
let x: u64 = 1; | ||
const CONST_X: i8 = 1; | ||
} | ||
} | ||
|
||
mod nested_local { | ||
fn test() { | ||
let x = { | ||
//~^ ERROR: type of this numeric variable is unconstrained | ||
let y = 1; | ||
//~^ ERROR: type of this numeric variable is unconstrained | ||
1 | ||
}; | ||
|
||
let x: i32 = { | ||
let y = 1; | ||
//~^ ERROR: type of this numeric variable is unconstrained | ||
1 | ||
}; | ||
|
||
const CONST_X: i32 = { | ||
let y = 1; | ||
//~^ ERROR: type of this numeric variable is unconstrained | ||
1 | ||
}; | ||
} | ||
} | ||
|
||
mod in_macro { | ||
use super::*; | ||
|
||
#[inline_macros] | ||
fn internal() { | ||
inline!(let x = 22;); | ||
//~^ ERROR: type of this numeric variable is unconstrained | ||
} | ||
|
||
fn external() { | ||
external!(let x = 22;); | ||
} | ||
} | ||
|
||
fn check_expect_suppression() { | ||
#[expect(clippy::unconstrained_numeric_literal)] | ||
let x = 21; | ||
} | ||
|
||
fn main() {} |
Oops, something went wrong.