Skip to content

Commit

Permalink
Fixed MAGIC_NUMBER (#1883)
Browse files Browse the repository at this point in the history
### What's done:
- Fixed case when configuration `ignoreLocalVariableDeclaration` didn't take into account variables with `val`.
- Fixed case when searching for properties using configuration `ignorePropertyDeclaration` also found ranges and local variables, for which there are corresponding configurations `ignoreRanges` and `ignoreLocalVariableDeclaration`.
- Added new configuration `ignorePairsCreatedUsingTo` for ignoring pairs created using `to`.
- Reworked warnings tests with many different configuration presets.

Closes #1826
  • Loading branch information
DrAlexD authored Dec 19, 2023
1 parent 0063c8b commit a0c564e
Show file tree
Hide file tree
Showing 10 changed files with 359 additions and 90 deletions.
4 changes: 3 additions & 1 deletion diktat-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@
# Ignore numbers from test
ignoreTest: "true"
# Ignore numbers
ignoreNumbers: "-1, 1, 0, 2"
ignoreNumbers: "-1, 1, 0, 2, 0U, 1U, 2U, -1L, 0L, 1L, 2L, 0UL, 1UL, 2UL"
# Is ignore override hashCode function
ignoreHashCodeFunction: "true"
# Is ignore property
Expand All @@ -342,6 +342,8 @@
ignoreRanges: "false"
# Is ignore number in extension function
ignoreExtensionFunctions: "false"
# Is ignore number in pairs created using to
ignorePairsCreatedUsingTo: "false"
# Checks that order of enum values or constant property inside companion is correct
- name: WRONG_DECLARATIONS_ORDER
enabled: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import org.jetbrains.kotlin.KtNodeTypes.OPERATION_REFERENCE
import org.jetbrains.kotlin.KtNodeTypes.PROPERTY
import org.jetbrains.kotlin.KtNodeTypes.VALUE_PARAMETER
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.lexer.KtTokens.IDENTIFIER
import org.jetbrains.kotlin.lexer.KtTokens.MINUS
import org.jetbrains.kotlin.lexer.KtTokens.RANGE
import org.jetbrains.kotlin.psi.KtFunction
Expand Down Expand Up @@ -52,23 +53,30 @@ class MagicNumberRule(configRules: List<RulesConfig>) : DiktatRule(
@Suppress("ComplexMethod")
private fun checkNumber(node: ASTNode, configuration: MagicNumberConfiguration) {
val nodeText = node.treePrev?.let { if (it.elementType == OPERATION_REFERENCE && it.hasChildOfType(MINUS)) "-${node.text}" else node.text } ?: node.text

val isIgnoreNumber = configuration.ignoreNumbers.contains(nodeText)

val isHashFunction = node.parent { it.elementType == FUN && it.isHashFun() } != null
val isConstant = node.parent { it.elementType == PROPERTY && it.isConstant() } != null
val isPropertyDeclaration = !isConstant && node.parent { it.elementType == PROPERTY && !it.isNodeFromCompanionObject() } != null
val isLocalVariable = node.parent { it.elementType == PROPERTY && it.isVarProperty() && (it.psi as KtProperty).isLocal } != null
val isLocalVariable = node.parent { it.elementType == PROPERTY && (it.isVarProperty() || it.isValProperty()) && (it.psi as KtProperty).isLocal } != null
val isValueParameter = node.parent { it.elementType == VALUE_PARAMETER } != null
val isConstant = node.parent { it.elementType == PROPERTY && it.isConstant() } != null
val isCompanionObjectProperty = node.parent { it.elementType == PROPERTY && it.isNodeFromCompanionObject() } != null
val isEnums = node.parent { it.elementType == ENUM_ENTRY } != null
val isRanges = node.treeParent.run {
this.elementType == BINARY_EXPRESSION &&
this.findChildByType(OPERATION_REFERENCE)?.hasChildOfType(RANGE) ?: false
val isRanges = node.treeParent.let {
it.elementType == BINARY_EXPRESSION && it.findChildByType(OPERATION_REFERENCE)?.hasChildOfType(RANGE) ?: false
}
val isExtensionFunctions = node.parent { it.elementType == FUN && (it.psi as KtFunction).isExtensionDeclaration() } != null &&
node.parents().none { it.elementType == PROPERTY }
val result = listOf(isHashFunction, isPropertyDeclaration, isLocalVariable, isValueParameter, isConstant,
isCompanionObjectProperty, isEnums, isRanges, isExtensionFunctions).zip(mapConfiguration.map { configuration.getParameter(it.key) })
if (result.any { it.first && it.first != it.second } && !isIgnoreNumber) {
val isPairsCreatedUsingTo = node.treeParent.let {
it.elementType == BINARY_EXPRESSION && it.findChildByType(OPERATION_REFERENCE)?.findChildByType(IDENTIFIER)?.text == "to"
}
val isPropertyDeclaration = !isLocalVariable && !isConstant && !isCompanionObjectProperty && !isRanges && !isPairsCreatedUsingTo &&
node.parent { it.elementType == PROPERTY } != null

val result = listOf(isHashFunction, isPropertyDeclaration, isLocalVariable, isValueParameter, isConstant, isCompanionObjectProperty, isEnums, isRanges,
isExtensionFunctions, isPairsCreatedUsingTo).zip(mapConfiguration.map { configuration.getParameter(it.key) })

if (result.any { it.first && !it.second } && !isIgnoreNumber) {
MAGIC_NUMBER.warn(configRules, emitWarn, nodeText, node.startOffset, node)
}
}
Expand Down Expand Up @@ -125,6 +133,7 @@ class MagicNumberRule(configRules: List<RulesConfig>) : DiktatRule(
"ignoreCompanionObjectPropertyDeclaration" to true,
"ignoreEnums" to false,
"ignoreRanges" to false,
"ignoreExtensionFunctions" to false)
"ignoreExtensionFunctions" to false,
"ignorePairsCreatedUsingTo" to false)
}
}
4 changes: 3 additions & 1 deletion diktat-rules/src/main/resources/diktat-analysis-huawei.yml
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@
# Ignore numbers from test
ignoreTest: "true"
# Ignore numbers
ignoreNumbers: "-1, 1, 0, 2"
ignoreNumbers: "-1, 1, 0, 2, 0U, 1U, 2U, -1L, 0L, 1L, 2L, 0UL, 1UL, 2UL"
# Is ignore override hashCode function
ignoreHashCodeFunction: "true"
# Is ignore property
Expand All @@ -339,6 +339,8 @@
ignoreRanges: "false"
# Is ignore number in extension function
ignoreExtensionFunctions: "false"
# Is ignore number in pairs created using to
ignorePairsCreatedUsingTo: "false"
# Checks that order of enum values or constant property inside companion is correct
- name: WRONG_DECLARATIONS_ORDER
enabled: true
Expand Down
4 changes: 4 additions & 0 deletions diktat-rules/src/main/resources/diktat-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@
ignorePropertyDeclaration: "false"
# Is ignore local variable
ignoreLocalVariableDeclaration: "false"
# Is ignore value parameter
ignoreValueParameter: "true"
# Is ignore constant
ignoreConstantDeclaration: "true"
# Is ignore property in companion object
Expand All @@ -337,6 +339,8 @@
ignoreRanges: "false"
# Is ignore number in extension function
ignoreExtensionFunctions: "false"
# Is ignore number in pairs created using to
ignorePairsCreatedUsingTo: "false"
# Checks that order of enum values or constant property inside companion is correct
- name: WRONG_DECLARATIONS_ORDER
enabled: true
Expand Down
Loading

0 comments on commit a0c564e

Please sign in to comment.