]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/approx_const.rs
modify code
[rust.git] / clippy_lints / src / approx_const.rs
index 069a10e9b3aeb7f8f1499457cff00709b164253c..e109ee0009ee0f624e725acad27ca90dbfc36995 100644 (file)
@@ -1,8 +1,8 @@
-use clippy_utils::diagnostics::span_lint;
+use clippy_utils::diagnostics::span_lint_and_help;
 use clippy_utils::{meets_msrv, msrvs};
 use rustc_ast::ast::{FloatTy, LitFloatType, LitKind};
 use rustc_hir::{Expr, ExprKind};
-use rustc_lint::{LateContext, LateLintPass, LintContext};
+use rustc_lint::{LateContext, LateLintPass};
 use rustc_semver::RustcVersion;
 use rustc_session::{declare_tool_lint, impl_lint_pass};
 use rustc_span::symbol;
     /// let x = std::f32::consts::PI;
     /// let y = std::f64::consts::FRAC_1_PI;
     /// ```
+    #[clippy::version = "pre 1.29.0"]
     pub APPROX_CONSTANT,
     correctness,
     "the approximate of a known float constant (in `std::fXX::consts`)"
 }
 
 // Tuples are of the form (constant, name, min_digits, msrv)
-const KNOWN_CONSTS: [(f64, &str, usize, Option<RustcVersion>); 18] = [
+const KNOWN_CONSTS: [(f64, &str, usize, Option<RustcVersion>); 19] = [
     (f64::E, "E", 4, None),
     (f64::FRAC_1_PI, "FRAC_1_PI", 4, None),
     (f64::FRAC_1_SQRT_2, "FRAC_1_SQRT_2", 5, None),
@@ -58,6 +59,7 @@
     (f64::LOG10_E, "LOG10_E", 5, None),
     (f64::PI, "PI", 3, None),
     (f64::SQRT_2, "SQRT_2", 5, None),
+    (f64::TAU, "TAU", 3, Some(msrvs::TAU)),
 ];
 
 pub struct ApproxConstant {
@@ -85,18 +87,16 @@ fn check_known_consts(&self, cx: &LateContext<'_>, e: &Expr<'_>, s: symbol::Symb
         let s = s.as_str();
         if s.parse::<f64>().is_ok() {
             for &(constant, name, min_digits, msrv) in &KNOWN_CONSTS {
-                if is_approx_const(constant, &s, min_digits)
+                if is_approx_const(constant, s, min_digits)
                     && msrv.as_ref().map_or(true, |msrv| meets_msrv(self.msrv.as_ref(), msrv))
                 {
-                    span_lint(
+                    span_lint_and_help(
                         cx,
                         APPROX_CONSTANT,
                         e.span,
-                        &format!(
-                            "approximate value of `{}::consts::{}` found. \
-                             Consider using it directly",
-                            module, &name
-                        ),
+                        &format!("approximate value of `{}::consts::{}` found", module, &name),
+                        None,
+                        "consider using the constant directly",
                     );
                     return;
                 }