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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 42198x 42198x 42198x 1x 21100x 21100x 21100x 21100x 63062x 63062x 63062x 63062x 63062x 63062x 63062x 63062x 63062x 63062x 63062x 63062x 63062x 63062x 63062x 63062x 63062x 63062x 63062x 63062x 63062x 63062x 63062x 63062x 63062x 63062x 63062x 63062x 63062x 63062x 63062x 21100x 21100x 20205x 20205x 21100x 21100x 374x 374x 21100x 21100x 411x 411x 411x 411x 3021x 3021x 3001x 3001x 20x 20x 3021x 9x 9x 11x 11x 11x 11x 411x 411x 411x 21100x 21100x 21100x 21100x 3021x 3021x 3021x 3021x 3021x 2987x 2987x 34x 34x 21100x 21100x 34x 34x 21100x 21100x 21100x 20x 22x 20x 20x 22x 21100x 21100x 21100x 20x 20x 9x 20x 4x 4x 7x 20x 1x 1x 6x 6x 6x 6x 6x 6x 4x 4x 6x 2x 2x 21100x 21100x 21100x 16x 16x 16x 21100x 21100x | 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 {Severity} from "../severity";
import {BuiltIn} from "../abap/5_syntax/_builtin";
export class StaleSubrcConf extends BasicRuleConfig {
public severity?: Severity = Severity.Warning;
}
export class StaleSubrc extends ABAPRule {
private conf = new StaleSubrcConf();
public getMetadata(): IRuleMetadata {
return {
key: "stale_subrc",
title: "sy-subrc checked after a method call",
shortDescription: `Reports IF, CHECK, ASSERT or CASE on sy-subrc directly after a method call without EXCEPTIONS`,
extendedInformation: `A method call without EXCEPTIONS does not set sy-subrc. After the call, sy-subrc holds whatever
the last statement inside the method set, so the check depends on the method's implementation
and breaks silently when the method changes.
Check the result of the method instead, eg. a returning parameter or an exception, or move the
check directly after the statement that sets sy-subrc.
Reported: the statement directly before the check is a method call, CALL METHOD, or an
assignment with a method call or NEW in its source.
Not reported: a call with EXCEPTIONS or EXCEPTION-TABLE, an assignment that only calls built-in
functions like lines( ), and any other statement before the check, eg. CALL FUNCTION or
CREATE OBJECT, a check that is the first statement of a block, or a check after ENDIF.
An assignment or CLEAR without a call does not change sy-subrc, so a check after it still
reads the statement before it, which is not reported either.
See also check_subrc and subrc_after_assign.`,
tags: [RuleTag.SingleFile],
badExample: `lv_parent = read_parent( ).
IF sy-subrc = 0.
WRITE lv_parent.
ENDIF.`,
goodExample: `lv_parent = read_parent( ).
IF lv_parent IS NOT INITIAL.
WRITE lv_parent.
ENDIF.`,
};
}
public getConfig() {
return this.conf;
}
public setConfig(conf: StaleSubrcConf) {
this.conf = conf;
}
public runParsed(file: ABAPFile): Issue[] {
const issues: Issue[] = [];
const statements = file.getStatements();
for (let i = 0; i < statements.length; i++) {
const statement = statements[i];
if (this.isSubrcCheck(statement) === false) {
continue;
}
const previous = this.previousStatement(i, statements);
if (previous === undefined || this.isCallWithoutExceptions(previous) === false) {
continue;
}
const message = "sy-subrc checked after a method call without EXCEPTIONS, the call does not set sy-subrc";
issues.push(Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity));
}
return issues;
}
////////////////
private isSubrcCheck(statement: StatementNode): boolean {
const s = statement.get();
if (!(s instanceof Statements.If
|| s instanceof Statements.Check
|| s instanceof Statements.Assert
|| s instanceof Statements.Case)) {
return false;
}
return this.readsSubrc(statement);
}
private readsSubrc(statement: StatementNode): boolean {
return statement.findAllExpressions(Expressions.FieldChain).some(f => f.concatTokens().toUpperCase() === "SY-SUBRC");
}
/** the statement directly before, skipping comments */
private previousStatement(index: number, statements: readonly StatementNode[]): StatementNode | undefined {
for (let i = index - 1; i >= 0; i--) {
if (!(statements[i].get() instanceof Comment)) {
return statements[i];
}
}
return undefined;
}
/** a standalone method call, or an assignment with a method call or NEW in its source */
private isCallWithoutExceptions(statement: StatementNode): boolean {
const s = statement.get();
if (s instanceof Statements.Call) {
return this.hasClassicExceptions(statement) === false;
} else if (!(s instanceof Statements.Move) || this.hasClassicExceptions(statement) === true) {
return false;
}
if (statement.findFirstExpression(Expressions.NewObject) !== undefined) {
return true;
}
const tokens = statement.getTokens();
for (const call of statement.findAllExpressions(Expressions.MethodCall)) {
const name = call.getFirstToken();
const index = tokens.indexOf(name);
const before = index > 0 ? tokens[index - 1].getStr() : "";
if (before === "->" || before === "=>" || BuiltIn.methods[name.getStr().toUpperCase()] === undefined) {
return true;
}
}
return false;
}
/** EXCEPTIONS, or EXCEPTION-TABLE which is lexed as EXCEPTION - TABLE */
private hasClassicExceptions(statement: StatementNode): boolean {
return statement.getTokens().some(t => t.getStr().toUpperCase() === "EXCEPTIONS")
|| statement.findFirstExpression(Expressions.MethodCallBody)?.findDirectTokenByText("EXCEPTION") !== undefined;
}
}
|