Skip to content

Commit

Permalink
Merge branch 'upstream_master'
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-kallen committed Feb 25, 2014
2 parents 9c668b5 + 8acf7df commit c24caa0
Show file tree
Hide file tree
Showing 222 changed files with 9,294 additions and 4,163 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// CS1105ExtensionMethodMustBeDeclaredStaticAction.cs
//
// Author:
// Mike Krüger <[email protected]>
//
// Copyright (c) 2014 Xamarin Inc. (http://xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using ICSharpCode.NRefactory.TypeSystem;
using System.Threading;
using System.Collections.Generic;
using System.Linq;

namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
[ContextAction("Extension methods must be declared static")]
public class CS1105ExtensionMethodMustBeDeclaredStaticAction : CodeActionProvider
{
public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var method = context.GetNode<MethodDeclaration>();
if (method == null || !method.NameToken.Contains(context.Location))
yield break;

if (method.HasModifier(Modifiers.Static))
yield break;
var param = method.Parameters.FirstOrDefault();
if (param == null || param.ParameterModifier != ParameterModifier.This)
yield break;
yield return new CodeAction(
context.TranslateString("Convert method to static"),
script => script.ChangeModifier(method, method.Modifiers | Modifiers.Static),
method) {
Severity = ICSharpCode.NRefactory.Refactoring.Severity.Error
};
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ protected override CodeAction GetAction(RefactoringContext context, AnonymousMet
Expression convertExpression = null;

var stmt = node.Body.Statements.FirstOrDefault();
if (stmt == null)
return null;
if (stmt.GetNextSibling(s => s.Role == BlockStatement.StatementRole) == null) {
var exprStmt = stmt as ExpressionStatement;
if (exprStmt != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ Expression GenerateTarget(RefactoringContext context, BinaryOperatorExpression b
{
var rr = context.Resolver.GetResolverStateBefore(bOp).LookupSimpleNameOrTypeName("Equals", emptyTypes, NameLookupMode.Expression) as MethodGroupResolveResult;
if (rr == null || rr.IsError || HasDifferentEqualsMethod (rr.Methods)) {
return new MemberReferenceExpression(
new PrimitiveType ("object"),
"Equals"
);
return new PrimitiveType ("object").Member("Equals");
}
return new IdentifierExpression("Equals");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
var methodDeclaration = CreateEventInvocatorAction.CreateEventInvocator (context, type, eventDeclaration, eventDeclaration.Variables.First (), resolvedType.GetDelegateInvokeMethod (), false);
var stmt = new ExpressionStatement (new InvocationExpression (
new IdentifierExpression (methodDeclaration.Name),
new MemberReferenceExpression (context.CreateShortType("System", "EventArgs"), "Empty")
context.CreateShortType("System", "EventArgs").Member("Empty")
));
script.InsertWithCursor(
context.TranslateString("Create event invocator"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
guessedType = GetDelegateType(context, ((MethodGroupResolveResult)resolveResult).Methods.First(), expr);
}
var name = CreateMethodDeclarationAction.CreateBaseName(expr, guessedType);
name = context.GetLocalNameProposal(name, expr.StartLocation);
var type = context.UseExplicitTypes ? context.CreateShortType(guessedType) : new SimpleType("var");
var varDecl = new VariableDeclarationStatement(type, name, expr.Clone());
var replaceNode = visitor.Matches.First () as Expression;
Expand Down Expand Up @@ -100,6 +101,7 @@ public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
}
var linkedNodes = new List<AstNode>();
var name = CreateMethodDeclarationAction.CreateBaseName(expr, guessedType);
name = context.GetLocalNameProposal(name, expr.StartLocation);
var type = context.UseExplicitTypes ? context.CreateShortType(guessedType) : new SimpleType("var");
var varDecl = new VariableDeclarationStatement(type, name, expr.Clone());
linkedNodes.Add(varDecl.Variables.First().NameToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
using System.Threading;
using ICSharpCode.NRefactory.TypeSystem;
using System.Threading.Tasks;
using Mono.CSharp;

namespace ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod
{
Expand All @@ -56,6 +57,8 @@ public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
foreach (var node in selected) {
if (!(node is Statement) && !(node is Comment) && !(node is NewLineNode) && !(node is PreProcessorDirective))
yield break;
if (node.DescendantNodesAndSelf().Any(n => n is YieldBreakStatement || n is YieldReturnStatement))
yield break;
}
var action = CreateFromStatements (context, new List<AstNode> (selected));
if (action != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
continue;
newSwitch.SwitchSections.Add(new SwitchSection() {
CaseLabels = {
new CaseLabel(new MemberReferenceExpression(target.Clone(), field.Name))
new CaseLabel(target.Clone().Member(field.Name))
},
Statements = {
new BreakStatement()
Expand Down Expand Up @@ -95,7 +95,7 @@ public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
foreach (var field in missingFields) {
script.InsertAfter(insertNode, new SwitchSection() {
CaseLabels = {
new CaseLabel(new MemberReferenceExpression(target.Clone(), field.Name))
new CaseLabel(target.Clone().Member(field.Name))
},
Statements = {
new BreakStatement()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
[ContextAction("Introduce format item", Description = "Creates a string.format call with the selection as parameter.")]
public class IntroduceFormatItemAction : CodeActionProvider
{
readonly static MemberReferenceExpression PrototypeFormatReference = new MemberReferenceExpression (new PrimitiveType ("string"), "Format");
readonly static MemberReferenceExpression PrototypeFormatReference = new PrimitiveType ("string").Member("Format");

public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
yield break;
}
yield return new CodeAction(context.TranslateString("Use string.Empty"), script => {
script.Replace(expr, new MemberReferenceExpression (new PrimitiveType ("string"), "Empty"));
script.Replace(expr, new PrimitiveType ("string").Member("Empty"));
}, expr);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
yield return new CodeAction(context.TranslateString("Use 'string.Format()'"),
script => {
var stringType = new PrimitiveType("string");
var formatInvocation = new InvocationExpression(
new MemberReferenceExpression(stringType, "Format"));
var formatInvocation = stringType.Invoke("Format");
var formatLiteral = new PrimitiveExpression("");
var counter = 0;
var arguments = new List<Expression>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ void CheckConversion(IType variableType, Expression expression)
var fd = expression.Parent.Parent as FieldDeclaration;
if (fd != null) {
fixes.Add(new CodeAction(
ctx.TranslateString("Change field return type"),
ctx.TranslateString("Change field type"),
script => {
script.Replace(fd.ReturnType, ctx.CreateTypeSystemAstBuilder(fd).ConvertType(rr.Type));
},
Expand All @@ -160,7 +160,7 @@ void CheckConversion(IType variableType, Expression expression)
var lc = expression.Parent.Parent as VariableDeclarationStatement;
if (lc != null) {
fixes.Add(new CodeAction(
ctx.TranslateString("Fix local return type"),
ctx.TranslateString("Change local variable type"),
script => {
script.Replace(lc.Type, new SimpleType("var"));
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,29 @@ internal static IType GetRequestedReturnType (BaseRefactoringContext ctx, AstNod
{
entityNode = returnStatement.GetParent(p => p is LambdaExpression || p is AnonymousMethodExpression || !(p is Accessor) && p is EntityDeclaration);
if (entityNode == null)
return null;
return SpecialType.UnknownType;
if (entityNode is EntityDeclaration) {
var rr = ctx.Resolve(entityNode) as MemberResolveResult;
if (rr == null)
return null;
return SpecialType.UnknownType;
if (((EntityDeclaration)entityNode).HasModifier(Modifiers.Async))
return TaskType.UnpackTask(ctx.Compilation, rr.Member.ReturnType);
return rr.Member.ReturnType;
}
bool isAsync = false;
if (entityNode is LambdaExpression)
isAsync = ((LambdaExpression)entityNode).IsAsync;
if (entityNode is AnonymousMethodExpression)
isAsync = ((AnonymousMethodExpression)entityNode).IsAsync;
foreach (var type in TypeGuessing.GetValidTypes(ctx.Resolver, entityNode)) {
if (type.Kind != TypeKind.Delegate)
continue;
var invoke = type.GetDelegateInvokeMethod();
if (invoke != null && !invoke.ReturnType.IsKnownType(KnownTypeCode.Void))
return invoke.ReturnType;
if (invoke != null) {
return isAsync ? TaskType.UnpackTask(ctx.Compilation, invoke.ReturnType) : invoke.ReturnType;
}
}
return null;
return SpecialType.UnknownType;
}


Expand Down Expand Up @@ -149,8 +157,10 @@ public override void VisitReturnStatement(ReturnStatement returnStatement)
return;
AstNode entityNode;
var rr = GetRequestedReturnType (ctx, returnStatement, out entityNode);
if (rr.Kind == TypeKind.Void)
return;
var actions = new List<CodeAction>();
if (rr != null) {
if (rr.Kind != TypeKind.Unknown) {
actions.Add(new CodeAction(ctx.TranslateString("Return default value"), script => {
Expression p;
if (rr.IsKnownType(KnownTypeCode.Boolean)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,71 +45,15 @@ protected override IGatherVisitor CreateVisitor(BaseRefactoringContext context)

class GatherVisitor : GatherVisitorBase<CS0127ReturnMustNotBeFollowedByAnyExpression>
{
bool skip;

public GatherVisitor (BaseRefactoringContext ctx) : base (ctx)
{
}

static bool AnonymousMethodMayReturnNonVoid(BaseRefactoringContext ctx, Expression anonymousMethodExpression)
{
foreach (var type in TypeGuessing.GetValidTypes(ctx.Resolver, anonymousMethodExpression)) {
if (type.Kind != TypeKind.Delegate)
continue;
var invoke = type.GetDelegateInvokeMethod();
if (invoke != null && !invoke.ReturnType.IsKnownType(KnownTypeCode.Void))
return true;
}
return false;
}

public override void VisitMethodDeclaration(MethodDeclaration methodDeclaration)
{
var primitiveType = methodDeclaration.ReturnType as PrimitiveType;
if (primitiveType == null || primitiveType.Keyword != "void")
return;
base.VisitMethodDeclaration(methodDeclaration);
}

public override void VisitOperatorDeclaration(OperatorDeclaration operatorDeclaration)
{
}

public override void VisitAccessor(Accessor accessor)
{
if (accessor.Role == PropertyDeclaration.SetterRole ||
accessor.Role == IndexerDeclaration.SetterRole )
base.VisitAccessor(accessor);
}


public override void VisitCustomEventDeclaration(CustomEventDeclaration eventDeclaration)
{
}

public override void VisitAnonymousMethodExpression(AnonymousMethodExpression anonymousMethodExpression)
{
bool old = skip;
skip = AnonymousMethodMayReturnNonVoid(ctx, anonymousMethodExpression);
base.VisitAnonymousMethodExpression(anonymousMethodExpression);
skip = old;
}

public override void VisitLambdaExpression(LambdaExpression lambdaExpression)
{
bool old = skip;
skip = AnonymousMethodMayReturnNonVoid(ctx, lambdaExpression);
base.VisitLambdaExpression(lambdaExpression);
skip = old;
}

public override void VisitReturnStatement(ReturnStatement returnStatement)
{
base.VisitReturnStatement(returnStatement);
if (skip)
return;

if (!returnStatement.Expression.IsNull) {
if (ctx.GetExpectedType(returnStatement.Expression).Kind == TypeKind.Void) {
var actions = new List<CodeAction>();
actions.Add(new CodeAction(ctx.TranslateString("Remove returned expression"), script => {
script.Replace(returnStatement, new ReturnStatement());
Expand Down
Loading

0 comments on commit c24caa0

Please sign in to comment.