-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Expand file tree
/
Copy pathFloatingPointLiteralPrecisionInspection.kt
More file actions
85 lines (76 loc) · 4.11 KB
/
FloatingPointLiteralPrecisionInspection.kt
File metadata and controls
85 lines (76 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.kotlin.idea.inspections
/**
* Highlight floating point literals that exceed precision of the corresponding type.
*
* Some floating point values can't be represented using IEEE 754 floating point standard.
* For example, the literal 9_999_999_999.000001 has the same representation as a Double
* as the literal 9_999_999_999.000002. Specifying excess digits hides the fact that
* computations use the rounded value instead of the exact constant.
*
* This inspection highlights floating point constants whose literal representation
* requires more precision than the floating point type can provide.
* It does not try to detect rounding errors or otherwise check computation results.
*/
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.K1Deprecation
import org.jetbrains.kotlin.idea.base.resources.KotlinBundle
import org.jetbrains.kotlin.idea.codeinsight.api.classic.inspections.AbstractKotlinInspection
import org.jetbrains.kotlin.idea.inspections.dfa.getKotlinType
import org.jetbrains.kotlin.psi.KtConstantExpression
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.expressionVisitor
import org.jetbrains.kotlin.psi.stubs.ConstantValueKind
import org.jetbrains.kotlin.psi.stubs.elements.KtConstantExpressionElementType
import org.jetbrains.kotlin.types.typeUtil.isFloat
import java.math.BigDecimal
@K1Deprecation
class FloatingPointLiteralPrecisionInspection : AbstractKotlinInspection() {
private object Holder {
val FLOAT_LITERAL: KtConstantExpressionElementType = KtConstantExpressionElementType.kindToConstantElementType(ConstantValueKind.FLOAT_CONSTANT)
val FORMATTING_CHARACTERS_REGEX: Regex = Regex("[_fF]")
}
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return expressionVisitor {
if ((it is KtConstantExpression) && (it.elementType == Holder.FLOAT_LITERAL)) {
val isFloat = it.getKotlinType()?.isFloat() ?: false
val uppercaseSuffix = isFloat && it.text?.endsWith('F') ?: false
val literal = it.text?.replace(Holder.FORMATTING_CHARACTERS_REGEX, "") ?: return@expressionVisitor
try {
val parseResult = if (isFloat)
literal.toFloat().toString()
else
literal.toDouble().toString()
val roundedValue = BigDecimal(parseResult)
val exactValue = BigDecimal(literal)
if (exactValue.compareTo(roundedValue) != 0) {
val replacementText = if (isFloat)
parseResult + if (uppercaseSuffix) "F" else "f"
else
parseResult
holder.registerProblem(
it,
KotlinBundle.message("floating.point.literal.precision.inspection"),
ProblemHighlightType.WEAK_WARNING,
FloatingPointLiteralPrecisionQuickFix(replacementText))
}
} catch (e: NumberFormatException) {
return@expressionVisitor
}
}
}
}
}
private class FloatingPointLiteralPrecisionQuickFix(val replacementText: String) : LocalQuickFix {
override fun getName(): String = KotlinBundle.message("replace.with.0", replacementText)
override fun getFamilyName(): String = name
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val element = descriptor.psiElement as? KtConstantExpression ?: return
element.replace(KtPsiFactory(project).createExpression(replacementText))
}
}