IG : Scope of variable is too broad
I really like the Scope of variable is too broad inspection,
and use it all the time to clean up messy code.
But the problem is that there are many limitations. Following
are some suggestions for improvement. Please comment.
I will file Jiras on these if they make sense.
Thanks,
-Alex
请先登录再写评论。
Scope Too Broad doesn't work if the variable was initiliazed
with null. Turning on "Allow Initilizer to construct objects"
has no effect.
Here is a contrived example:
I would like something in between allowing only primitive
initializers and the unsafe "Allow Initializer to construct objects."
As the inspection description notes, if the constructors have side effects,
then narrowing the scope of the object may change the behavior. I was
bitten by this myself once, so I had to turn this option off.
One example, is that shouldn't the constructors for the equivalent
primitive objects like String, Boolean, Integer, Float, Boolean
be allowed by default.
It seems the IG should always allow all the constructors for the
equivalent primitive objects like new Integer(), new Float(), new Boolean(),
etc.
See example 3 and 5 below. Currently a and be will only be flagged as
Scope Too Broad if "Allow initializer of variable to construct object"
is turned on.
Similarly for Strings.
More generally, is there some way through annotations or other mechanism
for us to flag constructors which have no side effects?
If the variable is assigned to a constant, it will not be flagged as
Scope Too Broad. This may be related to the assignment to null.
See Example 3.
Those are excellent suggestions, please file JIRA requests. Thanks for taking the time to look for ways to improve this inspection.
Bas
I filed some Jiras. I added some additional comments and examples.
-Alex
IG : Scope of variable is too broad : Support Null Initializers
http://www.jetbrains.net/jira/browse/IDEA-11688
IG : Scope of variable is too broad : Treat Primitive Wrappers interchangeable with primitive types
http://www.jetbrains.net/jira/browse/IDEA-11691
IG : Scope of variable is too broad : Support for Object Initializers
http://www.jetbrains.net/jira/browse/IDEA-11692