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

useless postfix expression isn't useless #7037

Closed
jbaron opened this issue Mar 11, 2024 · 1 comment
Closed

useless postfix expression isn't useless #7037

jbaron opened this issue Mar 11, 2024 · 1 comment
Labels
bug good first issue Issue that is easy to pickup for people that are new to the project help wanted rules

Comments

@jbaron
Copy link

jbaron commented Mar 11, 2024

Expected Behavior

Detekt should not complain about postfix expression being useless if it increases a value that lives outside the scope of the method it is used.

Observed Behavior

Detekt complaints that the postfix example below is useless, while in fact it does exactly the job it is suppose to do.

Steps to Reproduce

   private var ID = 0

  /**
   * Generate the next order id
   */
  @Synchronized
  private fun nextId(): String {
      return ID++.toString() // this is the line !!!!!
  }

Context

Not a big issue, just a false negative.

Your Environment

Maven build
detekt plugin in IntelliJ 2.3.0
Kotlin 1.9.22

@cortinico
Copy link
Member

This is a valid bug. The problem is here:

override fun visitReturnExpression(expression: KtReturnExpression) {
val postfixExpression = expression.returnedExpression?.asPostFixExpression()
if (postfixExpression != null && postfixExpression.shouldBeReported()) {
report(postfixExpression)
}
expression.returnedExpression
?.let(this::getPostfixExpressionChildren)
?.forEach(this::report)
}

Basically for return instructions we don't account for properties in the class, which we should.

This is up for grab if someone is looking for an easy contribution

@cortinico cortinico added the good first issue Issue that is easy to pickup for people that are new to the project label Mar 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug good first issue Issue that is easy to pickup for people that are new to the project help wanted rules
Projects
None yet
Development

No branches or pull requests

4 participants