Skip to content

Commit 7463252

Browse files
committed
implements #286
1 parent eb50aa1 commit 7463252

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
CLASS y_check_prefer_line_exists DEFINITION PUBLIC INHERITING FROM y_check_base CREATE PUBLIC.
2+
PUBLIC SECTION.
3+
METHODS constructor.
4+
5+
PROTECTED SECTION.
6+
METHODS inspect_tokens REDEFINITION.
7+
8+
METHODS get_statement_inline IMPORTING statement TYPE sstmnt
9+
RETURNING VALUE(result) TYPE string.
10+
11+
ENDCLASS.
12+
13+
14+
15+
CLASS y_check_prefer_line_exists IMPLEMENTATION.
16+
17+
18+
METHOD constructor.
19+
super->constructor( ).
20+
21+
settings-pseudo_comment = '"#EC PREF_LINE_EX' ##NO_TEXT.
22+
settings-disable_threshold_selection = abap_true.
23+
settings-threshold = 0.
24+
settings-prio = c_warning.
25+
settings-documentation = |{ c_docs_path-checks }prefer-line-exists.md|.
26+
27+
set_check_message( 'Prefer LINE_EXISTS to READ TABLE or LOOP AT!' ).
28+
ENDMETHOD.
29+
30+
31+
METHOD inspect_tokens.
32+
CHECK get_token_abs( statement-from ) = 'READ'
33+
OR get_token_abs( statement-from ) = 'LOOP'.
34+
35+
DATA(inline) = get_statement_inline( statement ).
36+
37+
IF inline NP '*TRANSPORTING NO FIELDS*'.
38+
RETURN.
39+
ENDIF.
40+
41+
DATA(configuration) = detect_check_configuration( statement ).
42+
43+
IF configuration IS INITIAL.
44+
RETURN.
45+
ENDIF.
46+
47+
raise_error( statement_level = statement-level
48+
statement_index = index
49+
statement_from = statement-from
50+
error_priority = configuration-prio ).
51+
ENDMETHOD.
52+
53+
54+
METHOD get_statement_inline.
55+
LOOP AT ref_scan_manager->tokens ASSIGNING FIELD-SYMBOL(<token>)
56+
FROM statement-from TO statement-to.
57+
result = |{ result } { <token>-str }|.
58+
ENDLOOP.
59+
ENDMETHOD.
60+
61+
ENDCLASS.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
*"* use this source file for your ABAP unit test classes
2+
CLASS ltc_read_table DEFINITION INHERITING FROM y_unit_test_base FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
3+
PROTECTED SECTION.
4+
METHODS get_cut REDEFINITION.
5+
METHODS get_code_with_issue REDEFINITION.
6+
METHODS get_code_without_issue REDEFINITION.
7+
METHODS get_code_with_exemption REDEFINITION.
8+
ENDCLASS.
9+
10+
CLASS ltc_read_table IMPLEMENTATION.
11+
12+
METHOD get_cut.
13+
result ?= NEW y_check_prefer_line_exists( ).
14+
ENDMETHOD.
15+
16+
METHOD get_code_with_issue.
17+
result = VALUE #(
18+
( 'REPORT y_example. ' )
19+
20+
( ' START-OF-SELECTION. ' )
21+
( ' DATA tadir TYPE TABLE OF tadir. ' )
22+
( ' DATA exists TYPE abap_bool. ' )
23+
24+
( | READ TABLE tadir TRANSPORTING NO FIELDS WITH KEY object = 'y_check_prefer_line_exists'. | )
25+
26+
( ' IF sy-subrc = 0. ' )
27+
( ' exists = abap_true. ' )
28+
( ' ENDIF. ' )
29+
).
30+
ENDMETHOD.
31+
32+
METHOD get_code_without_issue.
33+
result = VALUE #(
34+
( 'REPORT y_example. ' )
35+
36+
( ' START-OF-SELECTION. ' )
37+
( ' DATA tadir TYPE TABLE OF tadir. ' )
38+
( | DATA(exists) = xsdbool( line_exists( tadir[ object = 'y_check_prefer_line_exists' ] ) ). | )
39+
( | DATA(index) = line_index( tadir[ object = 'y_check_prefer_line_exists' ] ). | )
40+
).
41+
ENDMETHOD.
42+
43+
METHOD get_code_with_exemption.
44+
result = VALUE #(
45+
( 'REPORT y_example. ' )
46+
47+
( ' START-OF-SELECTION. ' )
48+
( ' DATA tadir TYPE TABLE OF tadir. ' )
49+
( ' DATA exists TYPE abap_bool. ' )
50+
51+
( | READ TABLE tadir TRANSPORTING NO FIELDS WITH KEY object = 'y_check_prefer_line_exists'. "#EC PREF_LINE_EX | )
52+
53+
( ' IF sy-subrc = 0. ' )
54+
( ' exists = abap_true. ' )
55+
( ' ENDIF. ' )
56+
).
57+
ENDMETHOD.
58+
59+
ENDCLASS.
60+
61+
62+
63+
CLASS ltc_loop_at DEFINITION INHERITING FROM ltc_read_table FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
64+
PROTECTED SECTION.
65+
METHODS get_code_with_issue REDEFINITION.
66+
METHODS get_code_with_exemption REDEFINITION.
67+
ENDCLASS.
68+
69+
CLASS ltc_loop_at IMPLEMENTATION.
70+
71+
METHOD get_code_with_issue.
72+
result = VALUE #(
73+
( 'REPORT y_example. ' )
74+
75+
( ' START-OF-SELECTION. ' )
76+
( ' DATA tadir TYPE TABLE OF tadir. ' )
77+
( ' DATA exists TYPE abap_bool. ' )
78+
79+
( | LOOP AT tadir TRANSPORTING NO FIELDS WHERE object = 'y_check_prefer_line_exists'. | )
80+
( ' exists = abap_true. ' )
81+
( ' ENDLOOP. ' )
82+
).
83+
ENDMETHOD.
84+
85+
METHOD get_code_with_exemption.
86+
result = VALUE #(
87+
( 'REPORT y_example. ' )
88+
89+
( ' START-OF-SELECTION. ' )
90+
( ' DATA tadir TYPE TABLE OF tadir. ' )
91+
( ' DATA exists TYPE abap_bool. ' )
92+
93+
( | LOOP AT tadir TRANSPORTING NO FIELDS WHERE object = 'y_check_prefer_line_exists'. "#EC PREF_LINE_EX | )
94+
( ' exists = abap_true. ' )
95+
( ' ENDLOOP. ' )
96+
).
97+
ENDMETHOD.
98+
99+
ENDCLASS.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<abapGit version="v1.0.0" serializer="LCL_OBJECT_CLAS" serializer_version="v1.0.0">
3+
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
4+
<asx:values>
5+
<VSEOCLASS>
6+
<CLSNAME>Y_CHECK_PREFER_LINE_EXISTS</CLSNAME>
7+
<LANGU>E</LANGU>
8+
<DESCRIPT>Prefer LINE_EXISTS to READ TABLE or LOOP AT</DESCRIPT>
9+
<STATE>1</STATE>
10+
<CLSCCINCL>X</CLSCCINCL>
11+
<FIXPT>X</FIXPT>
12+
<UNICODE>X</UNICODE>
13+
<WITH_UNIT_TESTS>X</WITH_UNIT_TESTS>
14+
</VSEOCLASS>
15+
</asx:values>
16+
</asx:abap>
17+
</abapGit>

0 commit comments

Comments
 (0)