Check if the string contains itself
V3062 An object ‘attributeName’ is used as an argument to its own method. Consider checking the first actual argument of the ‘Contains’ method. AWSSDK.MobileAnalytics.Net45 CustomEvent.cs 261
////// Dictionary that stores attribute for this event only. /// private Dictionary _attributes = new Dictionary(); ////// Gets the attribute. /// /// Attribute name. /// The attribute. Return null of attribute doesn't /// exist. public string GetAttribute(string attributeName) { if(string.IsNullOrEmpty(attributeName)) { throw new ArgumentNullException("attributeName"); } string ret = null; lock(_lock) { if(attributeName.Contains(attributeName)) ret = _attributes[attributeName]; } return ret; }
The analyzer has detected an error in the GetAttribute method: a string is checked whether it contains itself. From the description of the method it follows that if the attribute name (attributeName key) is found (in the dictionary _attributes), the attribute value should be returned, otherwise – null. In fact, as the condition attributeName.Contains(attributeName) is always true, an attempt is made to return the value by a key which might not be found in a dictionary. Then, instead of returning null, an exception KeyNotFoundException will be thrown.
Please click here to see more bugs from this project.