void VisitTypeCastExpression(Expression expression, IType exprType, IType castToType)
            {
                if (exprType.Kind == TypeKind.Unknown || castToType.Kind == TypeKind.Unknown || castToType.Kind == TypeKind.TypeParameter)
                {
                    return;
                }
                var foundConversion = conversion.ExplicitConversion(exprType, castToType);

                if (foundConversion == Conversion.None)
                {
                    AddIssue(new CodeIssue(expression, ctx.TranslateString("Type cast expression of incompatible type")));
                }
            }
Пример #2
0
            void CheckConversion(IType variableType, Expression expression)
            {
                if (variableType.Kind == TypeKind.Unknown)
                {
                    return;                     // ignore error if the variable type is unknown
                }
                if (ctx.GetConversion(expression).IsValid)
                {
                    return;                     // don't complain if the code is valid
                }
                var rr = ctx.Resolve(expression);

                if (rr.Type.Kind == TypeKind.Unknown)
                {
                    return;                     // ignore error if expression type is unknown
                }
                var foundConversion = conversion.ExplicitConversion(rr, variableType);

                var     builder            = ctx.CreateTypeSystemAstBuilder(expression);
                AstType variableTypeNode   = builder.ConvertType(variableType);
                AstType expressionTypeNode = builder.ConvertType(rr.Type);

                string            title;
                List <CodeAction> fixes = new List <CodeAction>();

                if (foundConversion.IsValid)
                {
                    // CS0266: An explicit conversion exists -> suggested fix is to insert the cast
                    title = string.Format(ctx.TranslateString("Cannot implicitly convert type `{0}' to `{1}'. An explicit conversion exists (are you missing a cast?)"),
                                          expressionTypeNode, variableTypeNode);
                    string          fixTitle  = string.Format(ctx.TranslateString("Cast to '{0}'"), variableTypeNode);
                    Action <Script> fixAction = script => {
                        var right     = expression.Clone();
                        var castRight = right.CastTo(variableTypeNode);
                        script.Replace(expression, castRight);
                    };
                    fixes.Add(new CodeAction(fixTitle, fixAction, expression));
                }
                else
                {
                    // CS0029: No explicit conversion -> Issue without suggested fix
                    title = string.Format(ctx.TranslateString("Cannot implicitly convert type `{0}' to `{1}'"),
                                          expressionTypeNode, variableTypeNode);
                }

                if (expression.Parent is VariableInitializer)
                {
                    var fd = expression.Parent.Parent as FieldDeclaration;
                    if (fd != null)
                    {
                        fixes.Add(new CodeAction(
                                      ctx.TranslateString("Change field type"),
                                      script => {
                            script.Replace(fd.ReturnType, ctx.CreateTypeSystemAstBuilder(fd).ConvertType(rr.Type));
                        },
                                      expression
                                      ));
                    }

                    var lc = expression.Parent.Parent as VariableDeclarationStatement;
                    if (lc != null)
                    {
                        fixes.Add(new CodeAction(
                                      ctx.TranslateString("Change local variable type"),
                                      script => {
                            script.Replace(lc.Type, new SimpleType("var"));
                        },
                                      expression
                                      ));
                    }
                }

                if (expression.Parent is ReturnStatement)
                {
                    AstNode entityNode;
                    var     type = CS0126ReturnMustBeFollowedByAnyExpression.GetRequestedReturnType(ctx, expression.Parent, out entityNode);
                    if (type != null)
                    {
                        var entity = entityNode as EntityDeclaration;
                        if (entity != null)
                        {
                            fixes.Add(new CodeAction(
                                          ctx.TranslateString("Change return type"),
                                          script => {
                                script.Replace(entity.ReturnType, ctx.CreateTypeSystemAstBuilder(entity).ConvertType(rr.Type));
                            },
                                          expression
                                          ));
                        }
                    }
                }
                AddIssue(new CodeIssue(expression, title, fixes));
            }
Пример #3
0
            bool IsValidReferenceOrBoxingConversion(IType fromType, IType toType)
            {
                Conversion c = conversions.ExplicitConversion(fromType, toType);

                return(c.IsValid && (c.IsIdentityConversion || c.IsReferenceConversion || c.IsBoxingConversion || c.IsUnboxingConversion));
            }