Skip to content

Commit c4cfafb

Browse files
Draft "NoImplicitFunctionReturnType"
1 parent a514f20 commit c4cfafb

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

src/main/kotlin/com/github/ivy/explicit/IvyExplicitRuleSetProvider.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.github.ivy.explicit
33
import com.github.ivy.explicit.rule.DataClassDefaultValuesRule
44
import com.github.ivy.explicit.rule.DataClassFunctionsRule
55
import com.github.ivy.explicit.rule.DataClassTypedIDsRule
6+
import com.github.ivy.explicit.rule.NoImplicitFunctionReturnTypeRule
67
import io.gitlab.arturbosch.detekt.api.Config
78
import io.gitlab.arturbosch.detekt.api.RuleSet
89
import io.gitlab.arturbosch.detekt.api.RuleSetProvider
@@ -16,7 +17,8 @@ class IvyExplicitRuleSetProvider : RuleSetProvider {
1617
listOf(
1718
DataClassFunctionsRule(config),
1819
DataClassDefaultValuesRule(config),
19-
DataClassTypedIDsRule(config)
20+
DataClassTypedIDsRule(config),
21+
NoImplicitFunctionReturnTypeRule(config),
2022
),
2123
)
2224
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.github.ivy.explicit.rule
2+
3+
import io.gitlab.arturbosch.detekt.api.*
4+
import org.jetbrains.kotlin.psi.KtNamedFunction
5+
6+
class NoImplicitFunctionReturnTypeRule(config: Config) : Rule(config) {
7+
8+
override val issue = Issue(
9+
"NoImplicitFunctionReturnType",
10+
Severity.Warning,
11+
"Functions and class methods should declare their return types explicitly to improve code readability and maintainability.",
12+
Debt.FIVE_MINS
13+
)
14+
15+
override fun visitNamedFunction(function: KtNamedFunction) {
16+
super.visitNamedFunction(function)
17+
// Check if the function has an explicit return type
18+
if (function.typeReference == null && !function.hasBlockBody() && !function.isLocal) {
19+
report(
20+
CodeSmell(
21+
issue,
22+
Entity.from(function),
23+
"The function '${function.name}' should declare an explicit return type."
24+
)
25+
)
26+
}
27+
}
28+
}
29+

src/main/resources/config/config.yml

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ IvyExplicit:
55
active: true
66
DataClassTypedIDs:
77
active: true
8+
NoImplicitFunctionReturnType:
9+
active: true

0 commit comments

Comments
 (0)