-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[java] ImmutableField false positive with multiple constructors #1151
Copy link
Copy link
Closed
Labels
good first issueA great starting point for new contributorsA great starting point for new contributorsin:documentationAffects the documentation [doc]Affects the documentation [doc]
Milestone
Description
Affects PMD Version: 6.3.0 and before
Rule: ImmutableField
Description:
The ImmutableField rule can detect false positives when you have multiple constructors that call this() on each other.
Code Sample demonstrating the false positive:
public class AnalogAccelerometer {
private AnalogInput m_analogChannel;
private boolean m_allocatedChannel; // Violation
public AnalogAccelerometer(final int channel) {
this(new AnalogInput(channel));
m_allocatedChannel = true;
}
public AnalogAccelerometer(final AnalogInput channel) {
m_allocatedChannel = false;
m_analogChannel = channel;
}
}Code Sample demonstrating the issue if I listened to PMD's recommendation:
public class AnalogAccelerometer {
private AnalogInput m_analogChannel;
private final boolean m_allocatedChannel;
public AnalogAccelerometer(final int channel) {
this(new AnalogInput(channel));
m_allocatedChannel = true; // error: variable m_allocatedChannel might already have been assigned
}
public AnalogAccelerometer(final AnalogInput channel) {
m_allocatedChannel = false;
m_analogChannel = channel;
}
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
good first issueA great starting point for new contributorsA great starting point for new contributorsin:documentationAffects the documentation [doc]Affects the documentation [doc]