Browse Source

Fix crash when 'return 1;' occurs within a constructor declaration.

pull/32/merge
Daniel Grunwald 13 years ago
parent
commit
d267602fec
  1. 20
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/CompilerErrors/CS0127ReturnMustNotBeFollowedByAnyExpression.cs
  2. 34
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/CS0127ReturnMustNotBeFollowedByAnyExpressionTests.cs

20
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/CompilerErrors/CS0127ReturnMustNotBeFollowedByAnyExpression.cs

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
//
//
// CS0127ReturnMustNotBeFollowedByAnyExpression.cs
//
// Author:
@ -43,7 +43,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -43,7 +43,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
class GatherVisitor : GatherVisitorBase
{
MethodDeclaration currentMethod;
string currentMethodName;
public GatherVisitor (BaseRefactoringContext ctx) : base (ctx)
{
@ -54,10 +54,22 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -54,10 +54,22 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var primitiveType = methodDeclaration.ReturnType as PrimitiveType;
if (primitiveType == null || primitiveType.Keyword != "void")
return;
currentMethod = methodDeclaration;
currentMethodName = methodDeclaration.Name;
base.VisitMethodDeclaration(methodDeclaration);
}
public override void VisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration)
{
currentMethodName = constructorDeclaration.Name;
base.VisitConstructorDeclaration(constructorDeclaration);
}
public override void VisitDestructorDeclaration(DestructorDeclaration destructorDeclaration)
{
currentMethodName = "~" + destructorDeclaration.Name;
base.VisitDestructorDeclaration(destructorDeclaration);
}
public override void VisitOperatorDeclaration(OperatorDeclaration operatorDeclaration)
{
}
@ -87,7 +99,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -87,7 +99,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
if (!returnStatement.Expression.IsNull) {
AddIssue(
returnStatement,
string.Format (ctx.TranslateString("`{0}': A return keyword must not be followed by any expression when method returns void"), currentMethod.Name),
string.Format (ctx.TranslateString("`{0}': A return keyword must not be followed by any expression when method returns void"), currentMethodName),
new CodeAction (
ctx.TranslateString("Remove returned expression"),
script => {

34
ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/CS0127ReturnMustNotBeFollowedByAnyExpressionTests.cs

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
//
//
// CS0127ReturnMustNotBeFollowedByAnyExpressionTests.cs
//
// Author:
@ -158,7 +158,37 @@ namespace ICSharpCode.NRefactory.CSharp.CodeIssues @@ -158,7 +158,37 @@ namespace ICSharpCode.NRefactory.CSharp.CodeIssues
Assert.AreEqual (0, issues.Count);
}
[Test]
public void TestConstructor ()
{
var input = @"class Foo
{
Foo ()
{
return 1;
}
}";
TestRefactoringContext context;
var issues = GetIssues (new CS0127ReturnMustNotBeFollowedByAnyExpression (), input, out context);
Assert.AreEqual (1, issues.Count);
}
[Test]
public void TestDestructor ()
{
var input = @"class Foo
{
~Foo ()
{
return 1;
}
}";
TestRefactoringContext context;
var issues = GetIssues (new CS0127ReturnMustNotBeFollowedByAnyExpression (), input, out context);
Assert.AreEqual (1, issues.Count);
}
}
}

Loading…
Cancel
Save