AutoRefactor icon indicating copy to clipboard operation
AutoRefactor copied to clipboard

Idea of a new rule "Assignment statement rather than assignment in expression"

Open nbauma109 opened this issue 4 years ago • 2 comments

This new rule would be the equivalent of the one with increment/decrement inside expression. https://github.com/JnRouvignac/AutoRefactor/blob/master/plugin/src/main/java/org/autorefactor/jdt/internal/ui/fix/ObsoleteIncrementStatementRatherThanIncrementExpressionCleanUp.java

As you may already know, Sonar raises a warning when assignement is inside an expression.

Example of non-compliant code candidate for refactoring

if ((str = cont.substring(pos1, pos2)).isEmpty()) { 
  //...

Desired refactoring result would be the Sonar-compliant version

str = cont.substring(pos1, pos2);
if (str.isEmpty()) {
  //...

Let me know you thoughts on it.

Would it be complicated to do?

nbauma109 avatar Jun 16 '21 18:06 nbauma109

What is the difference with the existing rule No assignment in if condition (ObsoleteNoAssignmentInIfConditionCleanUp.java)?

Fabrice-TIERCELIN avatar Jun 16 '21 18:06 Fabrice-TIERCELIN

Ah ok, I didn't notice that rule "No assignment in if condition (ObsoleteNoAssignmentInIfConditionCleanUp.java)" existed. So the idea would be to extend the principle to other expressions than if expressions, such as method invocations, array accesses,...

nbauma109 avatar Jun 16 '21 19:06 nbauma109