suggest casting int to long when Long is expecting

This commit is contained in:
anna
2013-11-28 12:16:22 +01:00
parent f2c576c7de
commit 5847e27156
5 changed files with 59 additions and 0 deletions

View File

@@ -90,6 +90,10 @@ public abstract class ArgumentFixerActionFactory {
if (parameterType instanceof PsiWildcardType) continue;
if (!GenericsUtil.isFromExternalTypeLanguage(parameterType)) continue;
if (suggestedCasts.contains(parameterType.getCanonicalText())) continue;
if (exprType instanceof PsiPrimitiveType && parameterType instanceof PsiClassType) {
parameterType = PsiPrimitiveType.getUnboxedType(parameterType);
if (parameterType == null) continue;
}
// strict compare since even widening cast may help
if (Comparing.equal(exprType, parameterType)) continue;
PsiCall newCall = (PsiCall) call.copy();

View File

@@ -66,6 +66,10 @@ public class CastMethodArgumentFix extends MethodArgumentFix {
parameterType = ((PsiPrimitiveType)parameterType).getBoxedType(context); //unboxing from type of cast expression will take place at runtime
if (parameterType == null) return false;
}
if (exprType instanceof PsiPrimitiveType && parameterType instanceof PsiClassType) {
parameterType = PsiPrimitiveType.getUnboxedType(parameterType);
if (parameterType == null) return false;
}
return parameterType.isConvertibleFrom(exprType);
}
}

View File

@@ -0,0 +1,8 @@
// "Cast parameter to 'long'" "true"
class a {
void f(Long l) {}
void g() {
f((long) 0);
}
}

View File

@@ -0,0 +1,8 @@
// "Cast parameter to 'long'" "true"
class a {
void f(Long l) {}
void g() {
f(<caret>0);
}
}

View File

@@ -0,0 +1,35 @@
/*
* 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.
*/
package com.intellij.codeInsight.daemon.quickFix;
public class CastMethodParameters15Test extends LightQuickFixTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
}
public void testPrimitiveWrappers() throws Exception {
doSingleTest(getTestName(false) + ".java");
}
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/castMethodParameters15";
}
}