2
// CS0127ReturnMustNotBeFollowedByAnyExpression.cs
5
// Mike Krüger <mkrueger@xamarin.com>
7
// Copyright (c) 2013 Xamarin Inc. (http://xamarin.com)
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
using System.Collections.Generic;
29
using ICSharpCode.NRefactory.PatternMatching;
31
namespace ICSharpCode.NRefactory.CSharp.Refactoring
33
[IssueDescription("CS0127: A method with a void return type cannot return a value.",
34
Description = "Since 'function' returns void, a return keyword must not be followed by an object expression",
35
Category = IssueCategories.CompilerErrors,
36
Severity = Severity.Error)]
37
public class CS0127ReturnMustNotBeFollowedByAnyExpression : ICodeIssueProvider
39
public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context)
41
return new GatherVisitor(context).GetIssues();
44
class GatherVisitor : GatherVisitorBase<CS0127ReturnMustNotBeFollowedByAnyExpression>
46
string currentMethodName;
48
public GatherVisitor (BaseRefactoringContext ctx) : base (ctx)
52
public override void VisitMethodDeclaration(MethodDeclaration methodDeclaration)
54
var primitiveType = methodDeclaration.ReturnType as PrimitiveType;
55
if (primitiveType == null || primitiveType.Keyword != "void")
57
currentMethodName = methodDeclaration.Name;
58
base.VisitMethodDeclaration(methodDeclaration);
61
public override void VisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration)
63
currentMethodName = constructorDeclaration.Name;
64
base.VisitConstructorDeclaration(constructorDeclaration);
67
public override void VisitDestructorDeclaration(DestructorDeclaration destructorDeclaration)
69
currentMethodName = "~" + destructorDeclaration.Name;
70
base.VisitDestructorDeclaration(destructorDeclaration);
73
public override void VisitOperatorDeclaration(OperatorDeclaration operatorDeclaration)
77
public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration)
81
public override void VisitIndexerDeclaration(IndexerDeclaration indexerDeclaration)
85
public override void VisitCustomEventDeclaration(CustomEventDeclaration eventDeclaration)
89
public override void VisitLambdaExpression(LambdaExpression lambdaExpression)
93
public override void VisitAnonymousMethodExpression(AnonymousMethodExpression anonymousMethodExpression)
97
public override void VisitReturnStatement(ReturnStatement returnStatement)
99
if (!returnStatement.Expression.IsNull) {
102
string.Format (ctx.TranslateString("`{0}': A return keyword must not be followed by any expression when method returns void"), currentMethodName),
104
ctx.TranslateString("Remove returned expression"),
106
script.Remove(returnStatement.Expression);