Hey @Belle,
Thanks for the feedback. You are right, this is a False Negative (FN). However, this one is very unlikely to be fixed anytime soon. The rule has been initially written to target lists of assignments, typically found in configuration classes, and repetitively setting values, many statements in a row.
class Config {
void zoo() {
animals.put("otter", "mammal");
animals.put("platypus", "mammal");
animals.put("beaver", "mammal");
// ...
animals.put("duck", "bird");
animals.put("platypus", "bird"); // issue S4143
}
}
Indeed, trying to handle what is in-between two assignments with the same key is extremely difficult with a static analysis approach, since we can not easily infer what are the consequences of the operations occurring. Example:
class A {
void foo(Map<String,String> map)) {
map.put("a", "start");
bar(); // what if the map is legitimately modified for key "a", or if there is some asynchronous stuff happening ?
map.put("a", "done");
}
void bar() {
// ...
}
We believe that if we try to update the rule to cover more cases, such as the one you describe, it will ultimately lead to introducing too many new False Positives (FP), winning only a few new True Positive (TP) in the battle, and at the cost of a now very complex rule to maintain on our hand.
At SonarSource, we try to maximize the rate of TPs over FPs… at the cost of some known FNs. Of course, we only do that in order to make sure that the issues that our analyzers raise always make total sense for our users, and we try to fix as much as possible any reported FPs. We believe that in the case of S4143, the ratio won’t be good if we try to cover more complex cases… and if users start not trusting the rule anymore, we all lose.
Cheers,
Michael