Skip to content

Commit f342c0c

Browse files
committed
fix NeedlessClearRule for multiple commands on same line (SAP#315)
1 parent 8209b0a commit f342c0c

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

com.sap.adt.abapcleaner/src/com/sap/adt/abapcleaner/parser/Command.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1829,7 +1829,15 @@ else if (token.getParentCommand() != this)
18291829
}
18301830
String insertText = ABAP.COMMENT_SIGN_STRING + " " + commentText.trim();
18311831
Token newComment = Token.create(firstInLine.lineBreaks, firstInLine.spacesLeft, insertText, firstInLine.sourceLineNum, language);
1832-
firstInLine.lineBreaks = 1;
1832+
1833+
// if this Command follows another Command on the same line, put both the comment and this Command to an own line
1834+
if (firstInLine == firstToken && firstInLine.lineBreaks == 0 && !isFirstCommandInCode()) {
1835+
int indent = getIndent();
1836+
firstInLine.setWhitespace(1, indent);
1837+
newComment.setWhitespace(1, indent);
1838+
} else {
1839+
firstInLine.lineBreaks = 1;
1840+
}
18331841

18341842
if (firstInLine.getPrev() == null) {
18351843
// check whether the comment was already created in a previous cleanup run, potentially checking multiple attached comments

com.sap.adt.abapcleaner/src/com/sap/adt/abapcleaner/programbase/CommandLineArgs.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class CommandLineArgs {
4545

4646
public static String[] getAllOptions() { return allOptions; }
4747

48+
@SuppressWarnings("unused")
4849
public static CommandLineArgs create(Persistency persistency, String[] args) {
4950
if (args == null || args.length == 0)
5051
return null;
@@ -110,7 +111,7 @@ public static CommandLineArgs create(Persistency persistency, String[] args) {
110111
targetDir = persistency.getAbsolutePath(nextArg);
111112

112113
} else if (arg.equals(OPT_LINE_RANGE)) {
113-
String lineRange = nextArg;
114+
String lineRange = (nextArg == null) ? "" : nextArg;
114115
int sepPos = lineRange.indexOf(LINE_RANGE_SEP);
115116
int startLine = (sepPos <= 0) ? -1 : Integer.valueOf(lineRange.substring(0, sepPos));
116117
int lastLine = (sepPos < 0 || sepPos + 1 >= lineRange.length()) ? -1 : Integer.valueOf(lineRange.substring(sepPos + 1));
@@ -163,8 +164,9 @@ public static CommandLineArgs create(Persistency persistency, String[] args) {
163164
}
164165

165166
// skip next argument, since it was already consumed above
166-
if (nextArg != null)
167+
if (nextArg != null) {
167168
++i;
169+
}
168170
}
169171

170172
if (sourceDir != null && sourceCode != null) {

test/com.sap.adt.abapcleaner.test/src/com/sap/adt/abapcleaner/rules/declarations/NeedlessClearTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,4 +409,19 @@ void testClearExportingParameterKept() {
409409

410410
testRule();
411411
}
412+
413+
@Test
414+
void testClearOnSameLineAsPrevCommand() {
415+
buildSrc(" DATA lo_any TYPE REF TO lcl_any.");
416+
buildSrc(" FREE lo_any. CLEAR lo_any.");
417+
418+
buildExp(" DATA lo_any TYPE REF TO lcl_any.");
419+
buildExp(" FREE lo_any.");
420+
buildExp(" \" TODO: remove needless CLEAR (ABAP cleaner)");
421+
buildExp(" CLEAR lo_any.");
422+
423+
putAnyMethodAroundSrcAndExp();
424+
425+
testRule();
426+
}
412427
}

0 commit comments

Comments
 (0)