All files / src/rules check_subrc.ts

94.52% Statements 207/219
85.05% Branches 74/87
100% Functions 11/11
94.52% Lines 207/219

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 2191x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20845x 20845x 20845x 20845x 20845x 20845x 20845x 20845x 20845x 20845x 20845x 20845x 1x 10423x 10423x 10423x 10423x 31108x 31108x 31108x 31108x 31108x 31108x 31108x 31108x 31108x 31108x 31108x 31108x 31108x 31108x 31108x 31108x 31108x 31108x 10423x 10423x 10188x 10188x 10423x 10423x 243x 243x 10423x 10423x 8x 8x 8x 8x 8x 10423x 10423x 282x 282x 282x 282x 282x 282x 1519x 1519x 1519x 1519x 1519x 1x 1x 1518x 1518x 1518x 1519x 1x 1x 1519x 1517x 1517x     1517x 1517x 1517x 1517x 1517x 3x 3x 1x 1x 2x 2x 1517x 1514x 1514x 1514x 1514x 1514x 1514x 1x 1x 1514x 1513x 1513x 1513x     1513x 1513x 1513x 1513x     1513x 1513x 1513x 1513x     1513x 1513x 1513x 1x 1x 1513x 1512x 1512x 1512x 1x 1x 1512x 1511x 1511x 1511x 3x 3x 3x 1519x 1519x 1519x 1519x 10423x 10423x 10423x 10423x 9x 8x 8x 8x 5x 5x 8x 4x 4x 10423x 10423x 4x 4x 4x 10423x 10423x 5x 1x 1x 1x   1x 1x   1x 1x 1x 1x 4x 4x 10423x 10423x 21x 21x 21x 21x 21x 8x 8x 8x 8x 8x 21x 21x 15x 15x 15x 2x 1x 1x 15x 13x 2x 13x 11x 1x 1x 11x     10x 11x 11x 15x 9x 9x 10423x 10423x
import * as Statements from "../abap/2_statements/statements";
import * as Expressions from "../abap/2_statements/expressions";
import {Issue} from "../issue";
import {BasicRuleConfig} from "./_basic_rule_config";
import {ABAPRule} from "./_abap_rule";
import {IRuleMetadata, RuleTag} from "./_irule";
import {StatementNode} from "../abap/nodes/statement_node";
import {Comment} from "../abap/2_statements/statements/_statement";
import {ABAPFile} from "../abap/abap_file";
import {EditHelper} from "../edit_helper";
 
export class CheckSubrcConf extends BasicRuleConfig {
  public openDataset: boolean = true;
  public authorityCheck: boolean = true;
  public selectSingle: boolean = true;
  public selectTable: boolean = true;
  public updateDatabase: boolean = true;
  public insertDatabase: boolean = true;
  public modifyDatabase: boolean = true;
  public readTable: boolean = true;
  public assign: boolean = true;
  public find: boolean = true;
}
 
export class CheckSubrc extends ABAPRule {
  private conf = new CheckSubrcConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "check_subrc",
      title: "Check sy-subrc",
      shortDescription: `Check sy-subrc`,
      extendedInformation: `Pseudo comment "#EC CI_SUBRC can be added to suppress findings
 
If sy-dbcnt is checked after database statements, it is considered okay.
 
"SELECT SINGLE @abap_true FROM " is considered as an existence check, also "SELECT COUNT( * )" is considered okay
 
If IS ASSIGNED is checked after assigning, it is considered okay.
 
FIND statement with MATCH COUNT is considered okay if subrc is not checked`,
      tags: [RuleTag.SingleFile, RuleTag.Quickfix],
      pseudoComment: "EC CI_SUBRC",
      pragma: "##SUBRC_OK",
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: CheckSubrcConf) {
    this.conf = conf;
  }
 
  private buildFix(file: ABAPFile, statement: StatementNode) {
    return {
      description: "Add ##SUBRC_OK",
      edit: EditHelper.insertAt(file, statement.getLastToken().getStart(), " ##SUBRC_OK"),
    };
  }
 
  public runParsed(file: ABAPFile): Issue[] {
    const issues: Issue[] = [];
    const statements = file.getStatements();
    const message = "Check sy-subrc";
    const config = this.getConfig();
 
    for (let i = 0; i < statements.length; i++) {
      const statement = statements[i];
 
// todo: CALL FUNCTION
 
      if (statement.getPragmas().some(t => t.getStr() === this.getMetadata().pragma)) {
        continue;
      }
 
      if (config.openDataset === true
          && statement.get() instanceof Statements.OpenDataset
          && this.isChecked(i, statements) === false) {
// it doesnt make sense to ignore the subrc for open dataset, so no quick fix
        issues.push(Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity));
      } else if (config.authorityCheck === true
          && statement.get() instanceof Statements.AuthorityCheck
          && this.isChecked(i, statements) === false) {
// it doesnt make sense to ignore the subrc for authority checks, so no quick fix
        issues.push(Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity));
      } else if (config.selectSingle === true
          && statement.get() instanceof Statements.Select
          && statement.concatTokens().toUpperCase().startsWith("SELECT SINGLE ")
          && this.isChecked(i, statements) === false
          && this.checksDbcnt(i, statements) === false) {
        const concat = statement.concatTokens().toUpperCase();
        if (concat.startsWith("SELECT SINGLE @ABAP_TRUE FROM ")) {
          continue;
        }
        const fix = this.buildFix(file, statement);
        issues.push(Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity, undefined, [fix]));
      } else if (config.selectTable === true
          && statement.get() instanceof Statements.Select
          && statement.concatTokens().toUpperCase().startsWith("SELECT SINGLE ") === false
          && statement.concatTokens().toUpperCase().startsWith("SELECT COUNT( * ) ") === false
          && statement.concatTokens().toUpperCase().startsWith("SELECT COUNT(*) ") === false
          && this.isChecked(i, statements) === false
          && this.checksDbcnt(i, statements) === false) {
        const fix = this.buildFix(file, statement);
        issues.push(Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity, undefined, [fix]));
      } else if (config.updateDatabase === true
          && statement.get() instanceof Statements.UpdateDatabase
          && this.isChecked(i, statements) === false
          && this.checksDbcnt(i, statements) === false) {
        const fix = this.buildFix(file, statement);
        issues.push(Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity, undefined, [fix]));
      } else if (config.insertDatabase === true
          && statement.get() instanceof Statements.InsertDatabase
          && this.isChecked(i, statements) === false
          && this.checksDbcnt(i, statements) === false) {
        const fix = this.buildFix(file, statement);
        issues.push(Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity, undefined, [fix]));
      } else if (config.modifyDatabase === true
          && statement.get() instanceof Statements.ModifyDatabase
          && this.isChecked(i, statements) === false
          && this.checksDbcnt(i, statements) === false) {
        const fix = this.buildFix(file, statement);
        issues.push(Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity, undefined, [fix]));
      } else if (config.readTable === true
          && statement.get() instanceof Statements.ReadTable
          && this.isChecked(i, statements) === false) {
        const fix = this.buildFix(file, statement);
        issues.push(Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity, undefined, [fix]));
      } else if (config.assign === true
          && statement.get() instanceof Statements.Assign
          && this.isSimpleAssign(statement) === false
          && this.isChecked(i, statements) === false) {
        const fix = this.buildFix(file, statement);
        issues.push(Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity, undefined, [fix]));
      } else if (config.find === true
          && statement.get() instanceof Statements.Find
          && this.isExemptedFind(statement) === false
          && this.isChecked(i, statements) === false) {
        const fix = this.buildFix(file, statement);
        issues.push(Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity, undefined, [fix]));
      }
    }
 
    return issues;
  }
 
////////////////
 
  private isSimpleAssign(statement: StatementNode): boolean {
    if (statement.getChildren().length === 5) {
      const source = statement.findDirectExpression(Expressions.AssignSource);
      if (source?.getChildren().length === 1
          && source.findDirectExpression(Expressions.Source) !== undefined) {
        return true;
      }
    }
    return false;
  }
 
  private isExemptedFind(s: StatementNode): boolean {
// see https://github.com/abaplint/abaplint/issues/2130
    return s.concatTokens().toUpperCase().includes(" MATCH COUNT ") === true;
  }
 
  private checksDbcnt(index: number, statements: readonly StatementNode[]): boolean {
    for (let i = index + 1; i < statements.length; i++) {
      const statement = statements[i];
      const concat = statement.concatTokens().toUpperCase();
      if (statement.get() instanceof Comment) {
        continue;
      } else if (statement.get() instanceof Statements.EndIf
          || statement.get() instanceof Statements.EndTestSeam) {
        continue;
      } else {
        return concat.includes("SY-DBCNT");
      }
    }
    return false;
  }
 
  private isChecked(index: number, statements: readonly StatementNode[]): boolean {
    let assigned: string | undefined = undefined;
    let assignedn: string | undefined = undefined;
 
    if (statements[index].get() instanceof Statements.Assign
        || statements[index].get() instanceof Statements.ReadTable) {
      const fs = statements[index].findFirstExpression(Expressions.FSTarget
      )?.findFirstExpression(Expressions.FieldSymbol)?.getFirstToken().getStr();
      assigned = fs?.toUpperCase() + " IS ASSIGNED";
      assignedn = fs?.toUpperCase() + " IS NOT ASSIGNED";
    }
 
    for (let i = index + 1; i < statements.length; i++) {
      const statement = statements[i];
      const concat = statement.concatTokens().toUpperCase();
      if (statement.get() instanceof Comment) {
        if (concat.includes("" + this.getMetadata().pseudoComment)) {
          return true;
        }
      } else if (statement.get() instanceof Statements.EndIf
          || statement.get() instanceof Statements.EndTestSeam) {
        continue;
      } else {
        if (assigned && concat.includes(assigned)) {
          return true;
        }
        if (assignedn && concat.includes(assignedn)) {
          return true;
        }
        return concat.includes(" SY-SUBRC")
          || concat.includes("CL_ABAP_UNIT_ASSERT=>ASSERT_SUBRC");
      }
    }
    return false;
  }
 
}