-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[java] LawOfDemeter: False positive with fields assigned to local vars #2188
Comments
Not sure, if I understand this correctly. In your code snippet, we assign a field to a local variable - the field is our own, that's why we should not flag access to So, it seems, that we don't consider fields when checking local variable initializers (I think, we check for method parameters, though). |
(It would help if you came back to me within a shorter period of the issue written up. It takes some time to recall why it was written up in the first place. ) I'd expect the field assignment was to a locally created instance, not created elsewhere as specified by the rule. Perhaps, the code sample ought to have examples of fields initialised in both ways to check whether that's the case. What are you driving at? Are you saying that all aliases i.e., using assignment operator (a new reference, if you may) should not be flagged as created elsewhere? Yes, of course. Add another check for that in the source if you like. If an object is created locally, all references to the object must be considered local as well. Why on earth would it be otherwise? As far as I can tell, there are two ways to tackle this. Track the original variable or reference and all aliases or references will have the same property i.e., created locally or externally. Or ignore all such assignments since the original reference will be flagged by the rule, anyway. The former involves more work and effort. Do you see any side effects of the latter? Evidently, further violations of Law of Demeter will not be flagged if an externally created object's alias is treated as a local variable. |
- Fixes pmd#2175 - Fixes pmd#2179 - Fixes pmd#1605: same fix as pmd#2179, since enum constants are static fields they are trusted. - Fixes pmd#2180: the fix is not to special-case Thread, but to consider all static methods as trusted (consistent with the treatment of static fields in pmd#2179) - Fixes pmd#2182: the fix is not to allow package-private access, but to allow a class to access fields of instances of the same class. - Fixes pmd#1014 - Fixes pmd#2188
This is fixed in PMD 7.0.0-rc1. It won't be backported to PMD 6. |
#2160 (comment)
Affects PMD Version:
6.20.0.
Rule:
LawOfDemeter.
Description:
Assigning an instance or local variables (created locally) should not be flagged as an object created elsewhere by this rule.
Code Sample demonstrating the issue:
Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]
The text was updated successfully, but these errors were encountered: