Uniformly handle HIR literals in visitors and lints · rust-lang/rust@663191d (original) (raw)

`@@ -3,10 +3,10 @@ use clippy_utils::diagnostics::span_lint_and_help;

`

3

3

`use clippy_utils::msrvs::{self, Msrv};

`

4

4

`use rustc_ast::ast::{FloatTy, LitFloatType, LitKind};

`

5

5

`use rustc_attr_parsing::RustcVersion;

`

6

``

`-

use rustc_hir::{Expr, ExprKind};

`

``

6

`+

use rustc_hir::{HirId, Lit};

`

7

7

`use rustc_lint::{LateContext, LateLintPass};

`

8

8

`use rustc_session::impl_lint_pass;

`

9

``

`-

use rustc_span::symbol;

`

``

9

`+

use rustc_span::{Span, symbol};

`

10

10

`use std::f64::consts as f64;

`

11

11

``

12

12

`declare_clippy_lint! {

`

`@@ -74,29 +74,15 @@ impl ApproxConstant {

`

74

74

`}

`

75

75

`}

`

76

76

``

77

``

`-

fn check_lit(&self, cx: &LateContext<'_>, lit: &LitKind, e: &Expr<'_>) {

`

78

``

`-

match *lit {

`

79

``

`-

LitKind::Float(s, LitFloatType::Suffixed(fty)) => match fty {

`

80

``

`-

FloatTy::F16 => self.check_known_consts(cx, e, s, "f16"),

`

81

``

`-

FloatTy::F32 => self.check_known_consts(cx, e, s, "f32"),

`

82

``

`-

FloatTy::F64 => self.check_known_consts(cx, e, s, "f64"),

`

83

``

`-

FloatTy::F128 => self.check_known_consts(cx, e, s, "f128"),

`

84

``

`-

},

`

85

``

`` -

// FIXME(f16_f128): add f16 and f128 when these types become stable.

``

86

``

`-

LitKind::Float(s, LitFloatType::Unsuffixed) => self.check_known_consts(cx, e, s, "f{32, 64}"),

`

87

``

`-

_ => (),

`

88

``

`-

}

`

89

``

`-

}

`

90

``

-

91

``

`-

fn check_known_consts(&self, cx: &LateContext<'_>, e: &Expr<'_>, s: symbol::Symbol, module: &str) {

`

``

77

`+

fn check_known_consts(&self, cx: &LateContext<'_>, span: Span, s: symbol::Symbol, module: &str) {

`

92

78

`let s = s.as_str();

`

93

79

`if s.parse::().is_ok() {

`

94

80

`for &(constant, name, min_digits, msrv) in &KNOWN_CONSTS {

`

95

81

`if is_approx_const(constant, s, min_digits) && msrv.is_none_or(|msrv| self.msrv.meets(msrv)) {

`

96

82

`span_lint_and_help(

`

97

83

` cx,

`

98

84

`APPROX_CONSTANT,

`

99

``

`-

e.span,

`

``

85

`+

span,

`

100

86

`` format!("approximate value of {module}::consts::{name} found"),

``

101

87

`None,

`

102

88

`"consider using the constant directly",

`

`@@ -111,9 +97,17 @@ impl ApproxConstant {

`

111

97

`impl_lint_pass!(ApproxConstant => [APPROX_CONSTANT]);

`

112

98

``

113

99

`impl<'tcx> LateLintPass<'tcx> for ApproxConstant {

`

114

``

`-

fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {

`

115

``

`-

if let ExprKind::Lit(lit) = &e.kind {

`

116

``

`-

self.check_lit(cx, &lit.node, e);

`

``

100

`+

fn check_lit(&mut self, cx: &LateContext<'_>, _hir_id: HirId, lit: &Lit, _negated: bool) {

`

``

101

`+

match lit.node {

`

``

102

`+

LitKind::Float(s, LitFloatType::Suffixed(fty)) => match fty {

`

``

103

`+

FloatTy::F16 => self.check_known_consts(cx, lit.span, s, "f16"),

`

``

104

`+

FloatTy::F32 => self.check_known_consts(cx, lit.span, s, "f32"),

`

``

105

`+

FloatTy::F64 => self.check_known_consts(cx, lit.span, s, "f64"),

`

``

106

`+

FloatTy::F128 => self.check_known_consts(cx, lit.span, s, "f128"),

`

``

107

`+

},

`

``

108

`` +

// FIXME(f16_f128): add f16 and f128 when these types become stable.

``

``

109

`+

LitKind::Float(s, LitFloatType::Unsuffixed) => self.check_known_consts(cx, lit.span, s, "f{32, 64}"),

`

``

110

`+

_ => (),

`

117

111

`}

`

118

112

`}

`

119

113

``