Skip to content

Commit 732976c

Browse files
authored
Merge pull request #265 from SAP/1.10.0
Version 1.10.0
2 parents 8605e88 + c7abed1 commit 732976c

34 files changed

+1946
-1330
lines changed

changelog.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,26 @@ Legend
88
+ : added
99
- : removed
1010

11+
Upgrade Note
12+
------------------
13+
Whenever you upgrade code pal for ABAP, it is highly recommended to execute the Y_CI_CHECK_REGISTRATION report to activate/reactivate the Checks (SE38 transaction) and regenerate the respective code inspector variant (SCI transaction)
14+
15+
2020-12-XX v1.10.0
16+
------------------
17+
+ New Check: Scope of the Variable (#276)
18+
* Comment position when code has pseudo comments (#282)
19+
* Y_CHECK_STATEMENT_POSITION: before DATA declarations (#272)
20+
+ 'was' and 'were' as a method which returns boolean (#275)
21+
! DB Access in UT has distinct behavior depending on test risk level (#267)
22+
! Prefer Case to ElseIf is now reporting single conditions only (#262)
23+
! Chain Declaration check is now warning by default (#266)
24+
+ abapLint app integration in GitHub (#264)
25+
+ API regression reports 'maintain attributes..." message (#259)
26+
+ API improvements (#259)
27+
* Missing space between string or character literal and parentheses (#253)
28+
* Broken UTs (#251)
29+
* Constants in Interface (#244)
30+
1131
2020-11-23 v1.09.0
1232
------------------
1333
+ deprecated classes/interfaces check

docs/check_documentation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
- [Cyclomatic Complexity](checks/cyclomatic-complexity.md)
1818
- [CX_ROOT Usage](checks/cx-root-usage.md)
1919
- [Database Access in Unit-Test](checks/db-access-in-ut.md)
20-
- [Declaration in IF](checks/declaration-in-if.md)
2120
- [Deprecated Classes](checks/deprecated-classes.md)
2221
- [Deprecated Key Word](checks/deprecated-key-word.md)
2322
- [Empty Catch](checks/empty_catch.md)
@@ -47,6 +46,7 @@
4746
- [READ TABLE with Subsequent Memory Assignment](checks/sub-assign-read-table.md)
4847
- [RECEIVING Statement Usage](checks/receiving-usage.md)
4948
- [Returning Name](checks/returning-name.md)
49+
- [Scope of Variable](checks/scope-of-variable.md)
5050
- [Self-Reference](checks/self-reference.md)
5151
- [TEST-SEAM Statement Usage](checks/test-seam-usage.md)
5252
- [Unit-Test Coverages (Statement, Branch and Procedure)](checks/unit-test-coverages.md)

docs/checks/check-statement-position.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
## CHECK Statement Position Check
66

77
### What is the Intent of the Check?
8-
The "Check Statement Position" verifies whether the `CHECK` statement is in the first position (first statement) within a method, function-module or form-routine.
9-
Do not use `CHECK` outside of the initialization section of a method. The statement behaves differently in different positions and may lead to unclear, unexpected effects.
8+
It verifies whether the `CHECK` statement is the first statement within a method, function-module, or form-routine.
109

11-
### Which attributes can be maintained?
12-
![Attributes](./imgs/check_statement_position.png)
10+
The [Clean ABAP](https://github.com/SAP/styleguides/blob/master/clean-abap/CleanABAP.md#avoid-check-in-other-positions) says:
11+
> Do not use `CHECK` outside of the initialization section of a method. The statement behaves differently in different positions and may lead to unclear, unexpected effects.
1312
1413
### How to solve the issue?
15-
The `CHECK` statement shall be the first statement of a method (suggested even before any DATA declaration);
16-
If not, try to substitute this keyword by an IF-statement instead.
14+
The `CHECK` statement shall be the first statement of a method. If it is not possible, try to substitute this keyword with an IF-statement instead.
1715

1816
### What to do in case of exception?
1917
In special cases you can suppress this finding by using the pseudo comment `"#EC CHECK_POSITION`.
@@ -43,7 +41,7 @@ METHOD example.
4341
CHECK sy-mandt = 000.
4442
...
4543
ENDMETHOD.
46-
```
44+
```
4745

4846
### Further Readings & Knowledge
4947
- [Avoid CHECK in other positions (Clean ABAP)](https://github.com/SAP/styleguides/blob/master/clean-abap/CleanABAP.md#avoid-check-in-other-positions)

docs/checks/declaration-in-if.md

Lines changed: 0 additions & 38 deletions
This file was deleted.
-6.52 KB
Binary file not shown.
-6.12 KB
Binary file not shown.

docs/checks/method-return-bool.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ The check searches for methods with a boolean returning value and then verifies
2222
* `ends_`
2323
* `must_`
2424
* `should_`
25+
* `was_`
26+
* `were_`
2527

2628
or if the name contains one of the following words:
2729

docs/checks/scope-of-variable.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# code pal for ABAP
2+
3+
[code pal for ABAP](../../README.md) > [Documentation](../check_documentation.md) > [Scope of Variable](scope-of-variable.md)
4+
5+
## Scope of Variable
6+
7+
### What is the Intent of the Check?
8+
If a variable is declared in a statement, it should be used/referred to inside this statement only (not outside).
9+
10+
### How does the check work?
11+
It searches for `DATA` and `FIELD-SYMBOLS` declaration inside of `IF`, `ELSEIF`, `ELSE`, `DO`, `CASE/WHEN`, `LOOP`, and `WHILE` statements, and for its usage/reference outside this statement.
12+
13+
### How to solve the issue?
14+
Relocate the declaration.
15+
16+
### What to do in case of exception?
17+
You can suppress Code Inspector findings generated by this check using the pseudo comment `"#EC SCOPE_OF_VAR`.
18+
The pseudo comment must be placed right after the variable usage/referece.
19+
20+
```abap
21+
IF has_entries = abap_true.
22+
DATA(value) = 1.
23+
ELSE.
24+
value = 2. "#EC SCOPE_OF_VAR
25+
ENDIF.
26+
```
27+
28+
### Example
29+
30+
Before:
31+
```abap
32+
IF has_entries = abap_true.
33+
DATA(value) = 1.
34+
ELSE.
35+
value = 2.
36+
ENDIF.
37+
```
38+
39+
After:
40+
```abap
41+
DATA value TYPE i.
42+
43+
IF has_entries = abap_true.
44+
value = 1.
45+
ELSE.
46+
value = 2.
47+
ENDIF.
48+
```
49+
50+
### Further Readings & Knowledge
51+
* [ABAP Styleguides on Clean Code](https://github.com/SAP/styleguides/blob/master/clean-abap/CleanABAP.md#dont-declare-inline-in-optional-branches)

src/checks/y_check_chain_decl_usage.clas.abap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ CLASS Y_CHECK_CHAIN_DECL_USAGE IMPLEMENTATION.
1919
settings-pseudo_comment = '"#EC CHAIN_DECL_USAG' ##NO_TEXT.
2020
settings-disable_threshold_selection = abap_true.
2121
settings-threshold = 0.
22+
settings-prio = c_warning.
2223
settings-documentation = |{ c_docs_path-checks }chain-declaration-usage.md|.
2324

2425
set_check_message( 'Do not chain up-front declarations!' ).

src/checks/y_check_check_stmnt_position.clas.abap

Lines changed: 51 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,24 @@ CLASS y_check_check_stmnt_position DEFINITION PUBLIC INHERITING FROM y_check_bas
44

55
PROTECTED SECTION.
66
METHODS inspect_tokens REDEFINITION.
7-
METHODS execute_check REDEFINITION.
87

98
PRIVATE SECTION.
10-
DATA statement_index TYPE i VALUE 0 ##NO_TEXT.
9+
METHODS is_not_relevant_token IMPORTING token TYPE string
10+
RETURNING VALUE(result) TYPE abap_bool.
1111

12-
METHODS is_token_excluded IMPORTING token_str TYPE stokesx-str
13-
RETURNING VALUE(result) TYPE abap_bool.
12+
METHODS has_wrong_position IMPORTING structure TYPE sstruc
13+
check TYPE sstmnt
14+
RETURNING VALUE(result) TYPE abap_bool.
15+
16+
METHODS is_check_in_loop IMPORTING structure TYPE sstruc
17+
check TYPE sstmnt
18+
RETURNING VALUE(result) TYPE abap_bool.
1419

1520
ENDCLASS.
1621

1722

1823

19-
CLASS Y_CHECK_CHECK_STMNT_POSITION IMPLEMENTATION.
24+
CLASS y_check_check_stmnt_position IMPLEMENTATION.
2025

2126

2227
METHOD constructor.
@@ -31,82 +36,63 @@ CLASS Y_CHECK_CHECK_STMNT_POSITION IMPLEMENTATION.
3136
ENDMETHOD.
3237

3338

34-
METHOD execute_check.
35-
LOOP AT ref_scan_manager->get_structures( ) ASSIGNING FIELD-SYMBOL(<structure>)
36-
WHERE stmnt_type EQ scan_struc_stmnt_type-form
37-
OR stmnt_type EQ scan_struc_stmnt_type-method
38-
OR stmnt_type EQ scan_struc_stmnt_type-function
39-
OR stmnt_type EQ scan_struc_stmnt_type-module
40-
OR type EQ scan_struc_type-event.
41-
42-
is_testcode = test_code_detector->is_testcode( <structure> ).
39+
METHOD inspect_tokens.
40+
CHECK get_token_abs( statement-from ) = 'CHECK'.
4341

44-
TRY.
45-
DATA(check_configuration) = check_configurations[ apply_on_testcode = abap_true ].
46-
CATCH cx_sy_itab_line_not_found.
47-
IF is_testcode EQ abap_true.
48-
CONTINUE.
49-
ENDIF.
50-
ENDTRY.
42+
CHECK has_wrong_position( structure = structure
43+
check = statement ) = abap_true.
5144

52-
DATA(index) = <structure>-stmnt_from.
53-
statement_index = 0.
45+
CHECK is_check_in_loop( structure = structure
46+
check = statement ) = abap_false.
5447

55-
LOOP AT ref_scan_manager->get_statements( ) ASSIGNING FIELD-SYMBOL(<statement>)
56-
FROM <structure>-stmnt_from TO <structure>-stmnt_to.
48+
DATA(check_configuration) = detect_check_configuration( statement ).
5749

58-
inspect_tokens( index = index
59-
statement = <statement> ).
50+
IF check_configuration IS INITIAL.
51+
RETURN.
52+
ENDIF.
6053

61-
index = index + 1.
62-
ENDLOOP.
63-
ENDLOOP.
54+
raise_error( statement_level = statement-level
55+
statement_index = index
56+
statement_from = statement-from
57+
error_priority = check_configuration-prio ).
6458
ENDMETHOD.
6559

6660

67-
METHOD inspect_tokens.
68-
LOOP AT ref_scan_manager->get_tokens( ) ASSIGNING FIELD-SYMBOL(<token>)
69-
FROM statement-from TO statement-from
70-
WHERE type NE scan_token_type-comment AND
71-
type NE scan_token_type-pragma.
72-
IF is_token_excluded( <token>-str ) EQ abap_true.
73-
RETURN.
74-
ENDIF.
75-
ENDLOOP.
76-
IF sy-subrc NE 0.
77-
RETURN.
78-
ENDIF.
79-
80-
statement_index = statement_index + 1.
61+
METHOD is_not_relevant_token.
62+
result = xsdbool( token EQ 'METHOD'
63+
OR token EQ 'FORM'
64+
OR token EQ 'FUNCTION'
65+
OR token EQ 'MODULE'
66+
OR token EQ 'DATA'
67+
OR token EQ 'TYPES'
68+
OR token EQ 'CHECK'
69+
OR token EQ 'FIELD-SYMBOLS' ).
70+
ENDMETHOD.
8171

82-
IF statement_index GT 1
83-
AND get_token_abs( statement-from ) = 'CHECK'.
8472

85-
DATA(check_configuration) = detect_check_configuration( statement ). "#EC DECL_IN_IF
73+
METHOD has_wrong_position.
74+
DATA(statements) = ref_scan_manager->get_statements( ).
8675

87-
IF check_configuration IS INITIAL.
76+
LOOP AT ref_scan_manager->get_statements( ) ASSIGNING FIELD-SYMBOL(<statement>)
77+
FROM structure-stmnt_from TO structure-stmnt_to.
78+
IF <statement>-number = check-number.
8879
RETURN.
8980
ENDIF.
90-
91-
raise_error( statement_level = statement-level
92-
statement_index = index
93-
statement_from = statement-from
94-
error_priority = check_configuration-prio ).
95-
ENDIF.
81+
IF is_not_relevant_token( get_token_abs( <statement>-from ) ) = abap_false.
82+
result = abap_true.
83+
RETURN.
84+
ENDIF.
85+
ENDLOOP.
9686
ENDMETHOD.
9787

9888

99-
METHOD is_token_excluded.
100-
result = xsdbool( token_str EQ 'METHOD' OR
101-
token_str EQ 'FORM' OR
102-
token_str EQ 'FUNCTION' OR
103-
token_str EQ 'MODULE' OR
104-
token_str EQ 'DATA' OR
105-
token_str EQ 'TYPES' OR
106-
token_str CP 'DATA(*)' OR
107-
token_str CP 'LOOP' OR
108-
( token_str EQ 'CHECK' AND statement_index EQ 0 ) ).
89+
METHOD is_check_in_loop.
90+
LOOP AT ref_scan_manager->get_tokens( ) ASSIGNING FIELD-SYMBOL(<token>)
91+
FROM structure-stmnt_from TO check-from
92+
WHERE str = 'LOOP'
93+
OR str = 'ENDLOOP'.
94+
result = xsdbool( <token>-str = 'LOOP' ).
95+
ENDLOOP.
10996
ENDMETHOD.
11097

111-
11298
ENDCLASS.

0 commit comments

Comments
 (0)