Suspicious getter: it returns a new mutable object on each access
Reports property getters that return a new mutable object on each access.
A property that holds a mutable object normally keeps one object. Every caller then reads and changes that same object.
A getter breaks this. It runs again on each access and returns a new object, so a caller changes an object that no other caller reads.
The inspection covers the coroutine objects Job, SupervisorJob, CompletableDeferred, CoroutineScope, MainScope, MutableStateFlow, MutableSharedFlow, Channel, Mutex and Semaphore.
It also covers a mutable collection factory such as mutableListOf or mutableMapOf.
Example:
To share one object, declare the property with an initializer:
If you want a factory, use a function instead. A name such as newJob() shows the intent to the caller.
Locating this inspection
- By ID
Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.
SuspiciousGetterForMutableObject- Via Settings dialog
Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.
Inspection ID: SuspiciousGetterForMutableObject
Suppressing Inspection
You can suppress this inspection by placing the following comment marker before the code fragment where you no longer want messages from this inspection to appear:
More detailed instructions as well as other ways and options that you have can be found in the product documentation:
Inspection Details | |
|---|---|
By default bundled with: |