Community
Participate
Working Groups
Created attachment 217877 [details] new quickfix: extract to null-checked local This is spawned from bug 337977 comment 18 to separate out changes that are meaningful only in the context of bug 331649, which in turn lives in a topic branch, not in master. From bug 337977 comment 18: Here's one more quick fix: when dereferencing a @Nullable field: - extract the field reference to a new local variable - add a null check surrounding the dereference (both now using the local) This is actually a matured version of an example from the JDT tutorial at this year's ECNA :) Thanks Ayush for the start! I'm intending to include this in the patch feature on behalf of bug 331649. At last this patch also contains some tests (for the new quickfix only). The quickfix considers the following: - statement inside a block / with no directly enclosing block - don't propose the quickfix inside a field initializer - don't propose names that are already in use in the current method/initializer - always add an else block with a TODO comment - if the statement to protect is a return, also add a return to the else block - handle various kinds of types - reference / primitive - arrays - parameterized types - various shapes of field references (single / qualified / this-qualified) - field reference inside structures like ternary, assignment-as-expression - dereference of array, indexed or ".length" I'm not sure if we can/should go into suggesting an if statement that covers several dereferences of the same field? Perhaps for subsequent occurrences we could propose to extend coverage of an existing if statement..
Oops, I completely forgot that I created this bug time ago. Anyway, let's continue in the new bug. *** This bug has been marked as a duplicate of bug 398995 ***