Can I make Visual Studio warn me when I forget to assign String.Replace()?

橙三吉。 提交于 2021-02-08 09:02:46

问题


I just wrote the following line of C# code, and then spent a couple of minutes troubleshooting when the program didn't work as expected:

myString.Replace(oldValue, newValue);

(myString, oldValue, and newValue are all string variables.)

Obviously my intent was to cause the myString variable to be reassigned a new string equal to the original myString value but with the oldValue substring replaced by newValue.

Equally obviously (in hindsight), that line of code didn't work because I should have assigned the return value of Replace() back to myString:

myString = myString.Replace(oldValue, newValue);

Even though I'm well aware that C# strings are immutable and therefore methods like Replace() are written to return a new string instance, I still find myself accidentally writing bad code like the original line of code above on occasion.

My question is: Can I get Visual Studio to give me a compile-time warning in this situation, to save me from needing to manually find and diagnose the problem at runtime?

Note that since I'm using a .Net framework method here (String.Replace), rewriting the method in question to use an out parameter (as in this similar question) isn't really an option.


回答1:


You can use ReSharper, it will give you a warning indicating that you are ignoring the return value from a method, when you don't assign the return value to a variable.




回答2:


I have created a custom FxCop rule that handles this case for ignoring the result from a call to an immutable type method.

the basic gist is this:

/// <summary>
/// We need to find all Pop nodes, these are a member of the ExpressionStatement class.
/// After we find a Pop statement (which ignores the current value on the stack), we will see if the value added
/// to the stack is an instance MethodCall on an ImmutableType.
/// </summary>
/// <param name="statement"></param>
public override void VisitExpressionStatement(ExpressionStatement statement)
{
    if (statement == null) { throw new ArgumentNullException("statement"); }

    if (statement.Expression.NodeType == NodeType.Pop)
    {
        VisitUnaryExpression(statement.Expression as UnaryExpression);
    }
    base.VisitExpressionStatement(statement);
}

/// <summary>
/// When we've found the UnaryExpression we check it for a MethodCall on an ImmutableType
/// </summary>
/// <param name="unaryExpression"></param>
public override void VisitUnaryExpression(UnaryExpression unaryExpression)
{
    if (unaryExpression == null) { throw new ArgumentNullException("unaryExpression"); }

    if (unaryExpression.NodeType == NodeType.Pop)
    {
        MethodCall call = unaryExpression.Operand as MethodCall;
        if (call != null)
        {
            MemberBinding binding = call.Callee as MemberBinding;
            if (binding.BoundMember.DeclaringType != null
                && immutableTypes.Contains(binding.BoundMember.DeclaringType.FullName))
            {
                Method method = binding.BoundMember as Method;

                // If the method also returns an immutable Type we flag it as a problem.
                if (immutableTypes.Contains(method.ReturnType.FullName))
                {
                    this.Problems.Add(new Problem(GetResolution(), call));
                }
            }
        }
    }
}


来源:https://stackoverflow.com/questions/26280971/can-i-make-visual-studio-warn-me-when-i-forget-to-assign-string-replace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!