Summary: Although try-with-resource is supported by nullsafe this code pattern throws it off and make nullsafe report on a virtual **b**yte-**c**ode variable. Check out debug output from `TryWithResource` (or attached visualisation of CFG): 0. node14: $bcvar2=null (on entry to try-with-resource). 1. node16: n$14=$bcvar2, but **also** PRUNE(!(n$14 == null), true). Then we go to 2. node18: do something here and in case of exception go to 3. node25->node23->node19->node20: and here we do $bcvar2->addSuppressed(...). Because on step 1 we refined nullability of n$14, but didn't refine nullability of $bcvar20, on step 3 we are sure that $bcvar is null and therefore issue an error. Reviewed By: mityal Differential Revision: D20558343 fbshipit-source-id: 520505039master
parent
31dd2884f6
commit
d8b0c99fa1
@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package codetoanalyze.java.nullsafe_default;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
|
||||
public class TryWithResource {
|
||||
private static final int[] KEYS = {1};
|
||||
|
||||
private void FP_OK_StringWriterInTWRBlock() throws IOException {
|
||||
// IMPORTANT: this for-loop is needed for issue to manifest.
|
||||
for (int key : KEYS) {
|
||||
// no-op
|
||||
}
|
||||
|
||||
try (StringWriter stringWriter = new StringWriter()) {
|
||||
// no-op
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue