add assert: respect initializer in ForStatement

This commit is contained in:
Anna Kozlova
2013-12-19 20:13:06 +04:00
parent ab1bebff33
commit 7c4a448e29
4 changed files with 62 additions and 0 deletions

View File

@@ -53,6 +53,10 @@ public class AddAssertStatementFix implements LocalQuickFix {
PsiElement element = descriptor.getPsiElement();
PsiElement anchorElement = PsiTreeUtil.getParentOfType(element, PsiStatement.class);
LOG.assertTrue(anchorElement != null);
final PsiElement tempParent = anchorElement.getParent();
if (tempParent instanceof PsiForStatement && !PsiTreeUtil.isAncestor(((PsiForStatement)tempParent).getBody(), anchorElement, false)) {
anchorElement = tempParent;
}
PsiElement prev = PsiTreeUtil.skipSiblingsBackward(anchorElement, PsiWhiteSpace.class);
if (prev instanceof PsiComment && JavaSuppressionUtil.getSuppressedInspectionIdsIn(prev) != null) {
anchorElement = prev;

View File

@@ -0,0 +1,8 @@
// "Assert 'container != null'" "true"
class A{
void test(){
Integer container = null;
assert container != null;
for (int i = 0, limit = container.intValue(); i < limit; i++){}
}
}

View File

@@ -0,0 +1,7 @@
// "Assert 'container != null'" "true"
class A{
void test(){
Integer container = null;
for (int i = 0, limit = con<caret>tainer.intValue(); i < limit; i++){}
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* User: anna
* Date: 21-Mar-2008
*/
package com.intellij.codeInsight.daemon.quickFix;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.dataFlow.DataFlowInspection;
import com.intellij.codeInspection.nullable.NullableStuffInspection;
import org.jetbrains.annotations.NotNull;
public class AddAssertStatementFixTest extends LightQuickFixTestCase {
@NotNull
@Override
protected LocalInspectionTool[] configureLocalInspectionTools() {
return new LocalInspectionTool[]{new DataFlowInspection()};
}
public void test() throws Exception {
doAllTests();
}
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/addAssert";
}
}