1 |
note |
2 |
description: "Summary description for {CA_SELF_ASSIGNMENT_RULE}." |
3 |
author: "" |
4 |
date: "$Date$" |
5 |
revision: "$Revision$" |
6 |
|
7 |
class |
8 |
CA_SELF_ASSIGNMENT_RULE |
9 |
|
10 |
inherit |
11 |
CA_RULE |
12 |
redefine |
13 |
title |
14 |
end |
15 |
|
16 |
create |
17 |
make |
18 |
|
19 |
feature {NONE} -- Initialization |
20 |
make |
21 |
do |
22 |
-- set the default parameters (subject to be changed by user) |
23 |
is_enabled := True |
24 |
create {CA_WARNING} severity |
25 |
create violations.make |
26 |
end |
27 |
|
28 |
feature {NONE} -- Activation |
29 |
|
30 |
register_actions (a_checker: CA_ALL_RULES_CHECKER) |
31 |
do |
32 |
a_checker.add_assign_pre_action (agent pre_assign) |
33 |
end |
34 |
|
35 |
feature -- Properties |
36 |
|
37 |
title: STRING |
38 |
once |
39 |
Result := "Self-assignment" |
40 |
end |
41 |
|
42 |
description: STRING |
43 |
once |
44 |
Result := "Assigning a variable to itself is a meaningless statement% |
45 |
% due to a typing error. Most probably, one of the two% |
46 |
% variable names was misspelled. One example among many% |
47 |
% others: the programmer wanted to assign a local variable% |
48 |
% to a class attribute and used one of the variable names twice." |
49 |
end |
50 |
|
51 |
options: LINKED_LIST[CA_RULE_OPTION] |
52 |
once |
53 |
create Result.make |
54 |
end |
55 |
|
56 |
is_system_wide: BOOLEAN |
57 |
once |
58 |
Result := False |
59 |
end |
60 |
|
61 |
format_violation_description (a_violation: CA_RULE_VIOLATION; a_formatter: TEXT_FORMATTER) |
62 |
do |
63 |
a_formatter.add_string ("Variable '") |
64 |
if attached {STRING_32} a_violation.long_description_info.first as l_name then |
65 |
a_formatter.add_string (l_name) |
66 |
else |
67 |
a_formatter.add_char ('?') |
68 |
end |
69 |
a_formatter.add_string ("' is assigned to itself. Assigning a variable to % |
70 |
%itself is a meaningless statement due to a typing% |
71 |
% error. Most probably, one of the two variable % |
72 |
%names was misspelled.") |
73 |
end |
74 |
|
75 |
feature {NONE} -- Checking the rule |
76 |
pre_assign (a_assign_as: ASSIGN_AS) |
77 |
local |
78 |
l_violation: CA_RULE_VIOLATION |
79 |
do |
80 |
if attached {EXPR_CALL_AS} a_assign_as.source as l_source then |
81 |
if attached {ACCESS_ID_AS} l_source.call as l_src_access_id then |
82 |
if attached {ACCESS_ID_AS} a_assign_as.target as l_tar |
83 |
and then l_tar.feature_name.is_equal (l_src_access_id.feature_name) then |
84 |
create l_violation.make_with_rule (Current) |
85 |
l_violation.set_affected_class (checking_class) |
86 |
l_violation.set_location (a_assign_as.start_location) |
87 |
l_violation.long_description_info.extend (l_src_access_id.feature_name.name_32) |
88 |
violations.extend (l_violation) |
89 |
end |
90 |
end |
91 |
end |
92 |
end |
93 |
|
94 |
end |