Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docs for AG001 #5

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions doc/AG001.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# AG001: Ignored Return Value Rule

This rule detects instances where return values from function calls
are ignored, which can lead to potential bugs.

## Description

Ignoring return values often indicates that a function is being called
solely for its side effects, which can be a code smell. This practice
can result in:

1. Wasted computation
2. Missed error handling
3. Unexpected behavior

## Noncompliant Code Examples

```kotlin
toUppercase("hello") // Noncompliant: Return value is ignored
getNumbers().filter { it > 2 } // Noncompliant: Return value is ignored
userManager.login("user", "password") // Noncompliant: Return value is ignored
```

## Compliant Code Examples

```kotlin
val uppercased = toUppercase("hello")
val filteredNumbers = getNumbers().filter { it > 2 }
val loginSuccessful = userManager.login("user", "password")
```

## Exceptions

The rule does not flag:
- Functions returning `Unit` or `Nothing`
- Cases where the return value is explicitly ignored using an underscore:

```kotlin
_ = toUppercase("hello") // Compliant: Explicitly ignored
```
We recognise that sometimes you do want to ignore the return value,
by making it explicit then we know its not a bug and it's intentional.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall
import org.jetbrains.kotlin.types.typeUtil.isNothing
import org.jetbrains.kotlin.types.typeUtil.isUnit

const val IGNORED_RETURN_VALUE: String = "AG001: The return value of this function call is ignored. see https://github.com/agoda-com/agoda-kraft/blob/main/doc/AG001.md"

@RequiresTypeResolution
class IgnoredReturnValueRule(config: Config) : Rule(config) {
override val issue = Issue(
javaClass.simpleName,
Severity.Warning,
"This rule reports when a function call's return value is ignored.",
"AG001: This rule reports when a function call's return value is ignored. see https://github.com/agoda-com/agoda-kraft/blob/main/doc/AG001.md",
Debt.FIVE_MINS
)


override fun visitCallExpression(expression: KtCallExpression) {
super.visitCallExpression(expression)

Expand Down Expand Up @@ -47,15 +50,15 @@ class IgnoredReturnValueRule(config: Config) : Rule(config) {
report(CodeSmell(
issue,
Entity.from(expression),
"The return value of this function call is ignored."
IGNORED_RETURN_VALUE
))
}
}
else -> {
report(CodeSmell(
issue,
Entity.from(expression),
"The return value of this function call is ignored."
IGNORED_RETURN_VALUE
))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class IgnoredReturnValueRuleTest(private val env: KotlinCoreEnvironment) {
"Test.kt\$returnsInt()"
)
findings.forEach { finding ->
assertThat(finding.message).isEqualTo("The return value of this function call is ignored.")
assertThat(finding.message).isEqualTo(IGNORED_RETURN_VALUE)
}
}

Expand Down