Skip to content

Commit

Permalink
proposition rule for nil branch
Browse files Browse the repository at this point in the history
this pull request propose a potential rule that check methods have not nil branch useless. #16515
  • Loading branch information
Hely authored and AngelHely committed May 13, 2024
1 parent 54f2c69 commit 5b9a313
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/General-Rules-Tests/ReNilBranchRuleTest.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Class {
#name : 'ReNilBranchRuleTest',
#superclass : 'ReAbstractRuleTestCase',
#category : 'General-Rules-Tests-Migrated',
#package : 'General-Rules-Tests',
#tag : 'Migrated'
}

{ #category : 'tests' }
ReNilBranchRuleTest >> testRuleNotViolated [

| critiques |
self class
compile: 'method 1 < 0 ifTrue: [ 1 ]'
classified: 'test-helper'.
[
critiques := self myCritiquesOnMethod: self class >> #method.
self assertEmpty: critiques ] ensure: [
(self class >> #method) removeFromSystem ]
]

{ #category : 'tests' }
ReNilBranchRuleTest >> testRuleWithIfFalse [

| critiques |
self class
compile: 'method 1 < 0 ifFalse: [ 1 ] ifTrue: [ nil ]'
classified: 'test-helper'.
[
critiques := self myCritiquesOnMethod: self class >> #method.
self assert: critiques size equals: 1 ] ensure: [
(self class >> #method) removeFromSystem ]
]

{ #category : 'tests' }
ReNilBranchRuleTest >> testRuleWithIfTrue [

| critiques |
self class
compile: 'method 1 < 0 ifTrue: [ 1 ] ifFalse: [ nil ]'
classified: 'test-helper'.
[
critiques := self myCritiquesOnMethod: self class >> #method.
self assert: critiques size equals: 1 ] ensure: [
(self class >> #method) removeFromSystem ]
]
32 changes: 32 additions & 0 deletions src/General-Rules/ReNilBranchRule.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"
this smell arise when a branch nil useless is detected.
"
Class {
#name : 'ReNilBranchRule',
#superclass : 'ReNodeRewriteRule',
#category : 'General-Rules-Migrated',
#package : 'General-Rules',
#tag : 'Migrated'
}

{ #category : 'accessing' }
ReNilBranchRule >> group [

^ 'Design Flaws'
]

{ #category : 'initialization' }
ReNilBranchRule >> initialize [

super initialize
replace: '`@condition ifTrue: [ `@.statements] ifFalse: [ nil ]'
with: '`@condition ifTrue: [ `@.statements]';
replace: '`@condition ifFalse: [ `@.statements] ifTrue: [ nil ]'
with: '`@condition ifFalse: [ `@.statements]'
]

{ #category : 'accessing' }
ReNilBranchRule >> name [

^ 'branch nil is useless'
]

0 comments on commit 5b9a313

Please sign in to comment.