Esempio n. 1
0
 private static bool MayReturnNull(MethodDeclarationSyntax node, SemanticModel model)
 {
     return(!(NullUtilities.ReturnsVoid(node) ||
              HasNullOrEmptyBody(node)) &&
            (HasCanBeNullAttribute(node) ||
             NullUtilities.ReturnsNull(node, model)));
 }
        public override SyntaxNode?VisitInvocationExpression(InvocationExpressionSyntax node)
        {
            var nullableArguments = node.ArgumentList.Arguments
                                    .Select((arg, index) => (Argument: arg, Index: index))
                                    .Where(indexedArg => NullUtilities.CanBeNull(indexedArg.Argument.Expression, _semanticModel))
                                    .ToList();

            if (nullableArguments.Count == 0)
            {
                return(node);
            }

            var methodDeclaration = _semanticModel.GetSymbolInfo(node);

            var methodSyntax = (MethodDeclarationSyntax?)methodDeclaration.Symbol?.DeclaringSyntaxReferences
                               .FirstOrDefault()?
                               .GetSyntaxAsync()?.GetAwaiter().GetResult();

            if (methodSyntax == null)
            {
                return(node);
            }

            foreach (var indexedArg in nullableArguments)
            {
                var nullableParameter = methodSyntax.ParameterList.Parameters[indexedArg.Index];
                _parameterCallback(nullableParameter);
            }

            return(node);
        }
Esempio n. 3
0
 public override SyntaxNode?VisitMethodDeclaration(MethodDeclarationSyntax node)
 {
     if (MayReturnNull(node, _semanticModel))
     {
         return(NullUtilities.ToNullReturning(node));
     }
     return(node);
 }
        private bool IsInitializedToNull(VariableDeclaratorSyntax variableDeclarator)
        {
            if (variableDeclarator.Initializer != null)
            {
                return(NullUtilities.CanBeNull(variableDeclarator.Initializer.Value, _semanticModel));
            }

            return(false);
        }
        public override SyntaxNode?VisitFieldDeclaration(FieldDeclarationSyntax node)
        {
            if (_uninitializedVariables.Contains(node))
            {
                return(node.WithDeclaration(
                           node.Declaration.WithType(NullUtilities.ToNullable(node.Declaration.Type))));
            }

            return(node);
        }
Esempio n. 6
0
        public override SyntaxNode?VisitPropertyDeclaration(PropertyDeclarationSyntax node)
        {
            if (HasCanBeNullAttribute(node) ||
                IsInClassOrStruct(node) &&
                (GetterReturnsNull(node) ||
                 IsUninitialized(node)))
            {
                return(node.WithType(NullUtilities.ToNullable(node.Type)));
            }

            return(node);
        }
Esempio n. 7
0
        public override SyntaxNode?VisitCastExpression(CastExpressionSyntax node)
        {
            var type = node.Type;

            if (type is NullableTypeSyntax)
            {
                return(node);
            }

            return(NullUtilities.CanBeNull(node.Expression, _semanticModel)
          ? node.WithType(NullUtilities.ToNullable(type))
          : node);
        }
        // GET: OrderDetail
        public ActionResult Details(int pId)
        {
            var result = _orderManager.OrderDetailRepo.FindByCondition(pOrderDetail => pOrderDetail.OrderId == pId);

            if (NullUtilities.IsNull(result))
            {
                return(RedirectToAction("Index"));
            }
            else
            {
                return(View(result));
            }
        }
Esempio n. 9
0
        // Edit page
        public ActionResult Edit(int pId)
        {
            var result = _productManager.ProductRepo.FindByCondition(p => p.Id == pId).SingleOrDefault();

            if (NullUtilities.IsNotNull(result))
            {
                return(View(result));
            }
            else
            {
                TempData["resultMessage"] = "Data error,Please try again!";
                return(RedirectToAction("Index"));
            }
        }
        public override SyntaxNode?VisitMethodDeclaration(MethodDeclarationSyntax node)
        {
            if (_nullableInterfaces.TryGetValue(node, out var parameterNames))
            {
                if (parameterNames.Contains("#return"))
                {
                    node = node.WithReturnType(NullUtilities.ToNullable(node.ReturnType));
                }
                var newParameterList = parameterNames.Aggregate(node.ParameterList, ToNullableParameter);
                return(node.WithParameterList(newParameterList));
            }

            return(base.VisitMethodDeclaration(node));
        }
Esempio n. 11
0
        public ActionResult MyOrder()
        {
            var userId = User.FindFirst(ClaimTypes.NameIdentifier).Value; // will give the user's userId
            var result = _orderManager.OrderRepo.FindByCondition(pOrder => pOrder.UserId == userId);

            if (NullUtilities.IsNotNull(result))
            {
                return(View(result));
            }
            else
            {
                return(View());
            }
        }
        public ActionResult Edit(string pId)
        {
            // user id is GUID
            var result = _userRepo.FindByCondition(p => p.Id == pId).SingleOrDefault();

            if (NullUtilities.IsNotNull(result))
            {
                return(View(result));
            }
            else
            {
                TempData["ResultMessage"] = String.Format("User [{0}] not exist,please retry.", pId);
                return(RedirectToAction("Index"));
            }
        }
        public ActionResult Details(int pId)
        {
            var result = _productManager.ProductRepo.FindByCondition(p => p.Id == pId).SingleOrDefault();

            if (NullUtilities.IsNull(result))
            {
                return(RedirectToAction("Index"));
            }
            else
            {
                var comments = _productManager.ProductCommentRepo.FindByCondition(pComment => pComment.ProductId == pId);
                ViewBag.Comment = comments;
                return(View(result));
            }
        }
Esempio n. 14
0
        public ActionResult Delete(int pId)
        {
            var result = _productManager.ProductRepo.FindByCondition(p => p.Id == pId).SingleOrDefault();

            if (NullUtilities.IsNotNull(result))
            {
                _productManager.ProductRepo.Delete(result);

                TempData["ResultMessage"] = String.Format("product [{0}] delete success", result.Name);
                return(RedirectToAction("Index"));
            }
            else
            {
                TempData["resultMessage"] = "Data not exist, please check again.";
            }
            return(RedirectToAction("Index"));
        }
        // Search Order By User
        public ActionResult SerachByUserName(string pSearch)
        {
            if (string.IsNullOrEmpty(pSearch))
            {
                return(RedirectToAction("Index"));
            }

            var result = _orderManager.OrderRepo.FindByCondition(pOrder => pOrder.UserName.Contains(pSearch, StringComparison.CurrentCultureIgnoreCase) ||
                                                                 pOrder.RecieverName.Contains(pSearch, StringComparison.CurrentCultureIgnoreCase)).ToList();

            if (NullUtilities.IsNotNull(result))
            {
                return(View("Index", result));
            }
            else
            {
                return(View("Index"));
            }
        }
Esempio n. 16
0
        public ActionResult MyOrderDetail(int pId)
        {
            var userId = User.FindFirst(ClaimTypes.NameIdentifier).Value; // will give the user's userId

            // check order belong current user
            if (_orderManager.OrderRepo.FindByCondition(pA => pA.Id == pId && pA.UserId == userId).Any() == false)
            {
                return(RedirectToAction("MyOrder"));
            }

            var result = _orderManager.OrderDetailRepo.FindByCondition(pOrderDetail => pOrderDetail.OrderId == pId);

            if (NullUtilities.IsNull(result))
            {
                return(RedirectToAction("MyOrder"));
            }
            else
            {
                return(View(result));
            }
        }
Esempio n. 17
0
        private bool GetterReturnsNull(PropertyDeclarationSyntax node)
        {
            var propertySymbol = _semanticModel.GetDeclaredSymbol(node);

            if (propertySymbol.GetMethod == null)
            {
                return(false);
            }

            var getter       = propertySymbol.GetMethod;
            var getterSyntax = getter.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() as AccessorDeclarationSyntax;

            if (getterSyntax?.Body != null)
            {
                return(NullUtilities.ReturnsNull(getterSyntax.Body.Statements, _semanticModel));
            }
            if (getterSyntax?.ExpressionBody != null)
            {
                return(NullUtilities.CanBeNull(getterSyntax.ExpressionBody.Expression, _semanticModel));
            }
            return(false);
        }
        public override SyntaxNode?VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
        {
            if (node.Declaration.Type.IsVar)
            {
                return(node);
            }

            if (node.Declaration.Type is NullableTypeSyntax)
            {
                return(node);
            }

            var type = node.Declaration.Type;

            var isNullable = node.Declaration.Variables
                             .Where(variable => variable.Initializer != null)
                             .Any(variable => NullUtilities.CanBeNull(variable.Initializer !.Value, _semanticModel));

            return(isNullable
          ? node.WithDeclaration(node.Declaration.WithType(NullUtilities.ToNullable(type)))
          : node);
        }
Esempio n. 19
0
        public override SyntaxNode?VisitMethodDeclaration(MethodDeclarationSyntax node)
        {
            var newParameters = node.ParameterList.Parameters;

            foreach (var parameter in node.ParameterList.Parameters)
            {
                if (IsNullableParameter(parameter))
                {
                    if (parameter.Type == null)
                    {
                        continue;
                    }

                    var toReplace = newParameters.SingleOrDefault(param => param.Identifier.ToString() == parameter.Identifier.ToString());
                    if (toReplace.Type != null)
                    {
                        newParameters = newParameters.Replace(toReplace, toReplace.WithType(NullUtilities.ToNullable(toReplace.Type)));
                    }
                }
            }

            return(node.WithParameterList(node.ParameterList.WithParameters(newParameters)));
        }
Esempio n. 20
0
 private bool IsNotNullInitialized(PropertyDeclarationSyntax node)
 {
     return(node.Initializer != null &&
            !NullUtilities.CanBeNull(node.Initializer.Value, _semanticModel));
 }