-
-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ReStatementsAfterReturnConditionalRule
this PR is a proposition for a new rule for "Method returning in conditional but still having statements". #6326
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/General-Rules-Tests/ReStatementsAfterReturnConditionalRuleTest.class.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
Class { | ||
#name : 'ReStatementsAfterReturnConditionalRuleTest', | ||
#superclass : 'ReAbstractRuleTestCase', | ||
#category : 'General-Rules-Tests-Migrated', | ||
#package : 'General-Rules-Tests', | ||
#tag : 'Migrated' | ||
} | ||
|
||
{ #category : 'tests' } | ||
ReStatementsAfterReturnConditionalRuleTest >> testRule [ | ||
|
||
| critiques | | ||
self class | ||
compile: 'method 1 = 1 ifTrue: [ ^ 1 ] ifFalse: [ ^ 2 ]. 2' | ||
classified: 'test-helper'. | ||
[ | ||
critiques := self myCritiquesOnMethod: self class >> #method. | ||
self assert: critiques size equals: 1 ] ensure: [ | ||
(self class >> #method) removeFromSystem ] | ||
] | ||
|
||
{ #category : 'tests' } | ||
ReStatementsAfterReturnConditionalRuleTest >> testRuleNotViolated [ | ||
|
||
| critiques | | ||
self class | ||
compile: 'method 1 = 1 ifTrue: [ ^ 1 ] ifFalse: [ ^ 2 ].' | ||
classified: 'test-helper'. | ||
[ | ||
critiques := self myCritiquesOnMethod: self class >> #method. | ||
self assertEmpty: critiques ] ensure: [ | ||
(self class >> #method) removeFromSystem ] | ||
] |
45 changes: 45 additions & 0 deletions
45
src/General-Rules/ReStatementsAfterReturnConditionalRule.class.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
" | ||
This smell arise when statements are written after a condition that return within 2 branch. | ||
For exemple : | ||
method | ||
1 = 1 | ||
fTrue: [ ^ 1 ] ifFalse: [ ^ 2 ]. | ||
2 | ||
" | ||
Class { | ||
#name : 'ReStatementsAfterReturnConditionalRule', | ||
#superclass : 'ReNodeBasedRule', | ||
#category : 'General-Rules-Migrated', | ||
#package : 'General-Rules', | ||
#tag : 'Migrated' | ||
} | ||
|
||
{ #category : 'running' } | ||
ReStatementsAfterReturnConditionalRule >> basicCheck: aNode [ | ||
|
||
aNode isMessage ifFalse: [ ^ false ]. | ||
(self selectorList includes: aNode selector) ifFalse: [ ^ false ]. | ||
aNode arguments do: [ :arg | | ||
(arg isBlock and: [ arg statements last isReturn ]) ifFalse: [ ^ false ] ]. | ||
^ aNode ~= aNode methodNode statements last | ||
] | ||
|
||
{ #category : 'accessing' } | ||
ReStatementsAfterReturnConditionalRule >> group [ | ||
|
||
^ 'Potential Bugs' | ||
] | ||
|
||
{ #category : 'accessing' } | ||
ReStatementsAfterReturnConditionalRule >> name [ | ||
|
||
^ 'statements written after conditional return' | ||
] | ||
|
||
{ #category : 'utilities' } | ||
ReStatementsAfterReturnConditionalRule >> selectorList [ | ||
|
||
^ #(#ifTrue:ifFalse: #ifFalse:ifTrue: #ifNil:ifNotNil: #ifNotNil:ifNil: #ifEmpty:ifNotEmpty: #ifNotEmpty:ifEmpty: #ifExists:ifAbsent:) | ||
] |