Skip to content

Commit

Permalink
Merge branch 'main' into ISSUE_112_recup
Browse files Browse the repository at this point in the history
  • Loading branch information
dedece35 authored Dec 4, 2023
2 parents d1a93d3 + 3470db6 commit ad8d17b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#132](https://github.com/green-code-initiative/ecoCode/issues/132) Upgrade RULES.md: set proposed Python rule "Use numpy array instead of standard list" as refused with link to the justification
- [#103](https://github.com/green-code-initiative/ecoCode/issues/103) Upgrade RULES.md: set proposed HTML rule "HTML page must contain a doctype tag" as refused with link to the justification
- [#112](https://github.com/green-code-initiative/ecoCode/issues/112) Updating EC1 rule to add controls on streams
- [#247](https://github.com/green-code-initiative/ecoCode/issues/247) Disable EC2 rule for float and double comparison for java language

### Deleted

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ private void computeConditionVariables(BinaryExpressionTree pBinExprTree, int pL
* @param pLevel the level of structure
*/
private void computeVariables(IdentifierTree pVarIdTree, int pLevel) {
if (pVarIdTree.is(Kind.IDENTIFIER)) {
if (pVarIdTree.is(Kind.IDENTIFIER)
&& !pVarIdTree.symbolType().is("float")
&& !pVarIdTree.symbolType().is("double")) {
// increment the variable counter to list of all variables
int nbUsed = variablesStruct.incrementVariableUsageForLevel(pVarIdTree.name(), pLevel);

Expand Down Expand Up @@ -217,18 +219,21 @@ private void visitElseNode(BlockTree pElseTree, int pLevel) {
*/
private void computeElseVariables(StatementTree pElseTree, int pLevel) {

for (Map.Entry<String, Integer> entry : variablesStruct.getVariablesForCurrentIfStruct(pLevel).entrySet()) {
String variableName = entry.getKey();
Map<String, Integer> mapVar = variablesStruct.getVariablesForCurrentIfStruct(pLevel);
if (mapVar != null) {
for (Map.Entry<String, Integer> entry : mapVar.entrySet()) {
String variableName = entry.getKey();

// increment usage of all variables in the same level of ELSE staetement
int nbUsed = variablesStruct.incrementVariableUsageForLevel(variableName, pLevel);
// increment usage of all variables in the same level of ELSE staetement
int nbUsed = variablesStruct.incrementVariableUsageForLevel(variableName, pLevel);

// increment variable counter to list of variables already declared for current if or elseif struture
variablesStruct.incrementVariableUsageForLevelForCurrentIfStruct(variableName, pLevel);
// increment variable counter to list of variables already declared for current if or elseif struture
variablesStruct.incrementVariableUsageForLevelForCurrentIfStruct(variableName, pLevel);

// raise an error if maximum
if (nbUsed > NB_MAX_VARIABLE_USAGE) {
reportIssue(pElseTree, ERROR_MESSAGE);
// raise an error if maximum
if (nbUsed > NB_MAX_VARIABLE_USAGE) {
reportIssue(pElseTree, ERROR_MESSAGE);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,40 @@ public int shouldBeCompliantBecauseSeveralVariablesUsedMaximumTwiceInIfOrElseIfS
return nb2;
}

// COMPLIANT
// USE CASE : Compliant use case to check if following is OK :
// - usage of the same variable on different levels of IF statements but with incompatible type for a switch
public float shouldBeCompliantBecauseVariableHasNotCompatibleTypeFloatForSwitch()
{
float nb1 = 0.0f;

if (nb1 > 1) {
nb1 = 2.1f;
} else {
if (nb1 > 2) {
nb1 = 1.1f;
}
}

return nb1;
}

// COMPLIANT
// USE CASE : Compliant use case to check if following is OK :
// - usage of the same variable on different levels of IF statements but with incompatible type for a switch
public double shouldBeCompliantBecauseVariableHasNotCompatibleTypeDoubleForSwitch()
{
double nb1 = 0.0;

if (nb1 > 1) {
nb1 = 2.1;
} else {
if (nb1 > 2) {
nb1 = 1.1;
}
}

return nb1;
}

}

0 comments on commit ad8d17b

Please sign in to comment.