|
| 1 | +/* |
| 2 | + * Sonar Delphi Plugin |
| 3 | + * Copyright (C) 2025 Integrated Application Development |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU Lesser General Public |
| 7 | + * License as published by the Free Software Foundation; either |
| 8 | + * version 3 of the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + * Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public |
| 16 | + * License along with this program; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 |
| 18 | + */ |
| 19 | +package au.com.integradev.delphi.checks; |
| 20 | + |
| 21 | +import org.sonar.check.Rule; |
| 22 | +import org.sonar.plugins.communitydelphi.api.ast.ExpressionNode; |
| 23 | +import org.sonar.plugins.communitydelphi.api.ast.ForToStatementNode; |
| 24 | +import org.sonar.plugins.communitydelphi.api.ast.IntegerLiteralNode; |
| 25 | +import org.sonar.plugins.communitydelphi.api.ast.NameReferenceNode; |
| 26 | +import org.sonar.plugins.communitydelphi.api.ast.PrimaryExpressionNode; |
| 27 | +import org.sonar.plugins.communitydelphi.api.check.DelphiCheck; |
| 28 | +import org.sonar.plugins.communitydelphi.api.check.DelphiCheckContext; |
| 29 | +import org.sonar.plugins.communitydelphi.api.reporting.QuickFix; |
| 30 | +import org.sonar.plugins.communitydelphi.api.reporting.QuickFixEdit; |
| 31 | +import org.sonar.plugins.communitydelphi.api.symbol.declaration.NameDeclaration; |
| 32 | +import org.sonar.plugins.communitydelphi.api.symbol.declaration.RoutineNameDeclaration; |
| 33 | +import org.sonar.plugins.communitydelphi.api.symbol.declaration.TypedDeclaration; |
| 34 | + |
| 35 | +@Rule(key = "IterationPastHighBound") |
| 36 | +public class IterationPastHighBoundCheck extends DelphiCheck { |
| 37 | + @Override |
| 38 | + public DelphiCheckContext visit(ForToStatementNode forStatement, DelphiCheckContext context) { |
| 39 | + var target = forStatement.getTargetExpression().skipParentheses(); |
| 40 | + var initializer = forStatement.getInitializerExpression().skipParentheses(); |
| 41 | + |
| 42 | + if (isZero(initializer) && isSuspiciousTargetExpression(target)) { |
| 43 | + context |
| 44 | + .newIssue() |
| 45 | + .onNode(target) |
| 46 | + .withMessage("Ensure this for loop is not iterating beyond the end of the collection.") |
| 47 | + .withQuickFixes( |
| 48 | + QuickFix.newFix("Subtract one from expression") |
| 49 | + .withEdit(QuickFixEdit.insertAfter(" - 1", target))) |
| 50 | + .report(); |
| 51 | + } |
| 52 | + |
| 53 | + return super.visit(forStatement, context); |
| 54 | + } |
| 55 | + |
| 56 | + private boolean isZero(ExpressionNode expression) { |
| 57 | + if (!(expression instanceof PrimaryExpressionNode)) { |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + var child = expression.getChild(0); |
| 62 | + return child instanceof IntegerLiteralNode |
| 63 | + && ((IntegerLiteralNode) child).getValue().intValue() == 0; |
| 64 | + } |
| 65 | + |
| 66 | + private boolean isSuspiciousTargetExpression(ExpressionNode expression) { |
| 67 | + if (!(expression instanceof PrimaryExpressionNode)) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + var reference = expression.getChild(0); |
| 72 | + if (!(reference instanceof NameReferenceNode)) { |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + var declaration = ((NameReferenceNode) reference).getLastName().getNameDeclaration(); |
| 77 | + return isArrayLength(declaration) || isCount(declaration); |
| 78 | + } |
| 79 | + |
| 80 | + private boolean isCount(NameDeclaration declaration) { |
| 81 | + if (!(declaration instanceof TypedDeclaration)) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + return declaration.getName().equalsIgnoreCase("Count") |
| 86 | + && ((TypedDeclaration) declaration).getType().isInteger(); |
| 87 | + } |
| 88 | + |
| 89 | + private boolean isArrayLength(NameDeclaration declaration) { |
| 90 | + if (!(declaration instanceof RoutineNameDeclaration)) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + var routine = (RoutineNameDeclaration) declaration; |
| 95 | + return routine.fullyQualifiedName().equals("System.Length") |
| 96 | + && routine.getParameters().get(0).getType().is("<array>"); |
| 97 | + } |
| 98 | +} |
0 commit comments