Question regarding search/replace with pattern

From C# < 4 I have a lot of code that for string parameters doesn't allow either null or a string with just whitespace (or just an empty string).

So I have lots of code that looks exactly like this:

    if (Object.ReferenceEquals(null, parameterName) || parameterName.Trim().Length == 0)
        throw new ArgumentNullException("parameterName");

Now, I would like to use search/replace with pattern to change the above code to:

    if (string.IsNullOrWhitespace(parameterName))

        throw new ArgumentNullException("parameterName");




So I tried adding the following:



    search for: Object.ReferenceEquals(null, $name$) || $name$.Trim().Length == 0
    $name$ is set as identifier, no regex



Replace with:



    string.IsNullOrWhitespace($name$)



Now, I can use the search with pattern dialogs (both the search and the catalog dialogs) to *find* these occurances, but no matter what I do, replace does not work.

Additionally, after adding it to the catalog, and specifying "show as warning", it still does not show up in the Alt+Enter quickfix popup (am I correct in assuming it should've appeared here?).

What am I doing wrong, or misunderstanding?
3 comments
Comment actions Permalink

Hello Lasse
     Could you please attach your pattern (imported as an xml file using 'Import' button on the 'Pattern Catalogue' window) and a small sample solution you've tested it with? Thank you!

Andrey Serebryansky

Support Engineer

JetBrains, Inc

http://www.jetbrains.com

"Develop with pleasure!"
0
Comment actions Permalink

1. Create a new class library project for .NET 4.0 in Visual Studio 2010
2. Replace the contents of Class1.cs (or add the file) with the following:


using System;

namespace SearchPatternTest
{
    public class Class1
    {
        public void Test(string name)
        {
            if (Object.ReferenceEquals(null, name) || name.Trim().Length == 0)
                throw new ArgumentNullException("name");
        }
    }
}


3. Import the following pattern:

<CustomPatterns>
  <Pattern Severity="WARNING">
    <Comment>Expanded string-check against null/empty/whitespace</Comment>
    <ReplaceComment>Use string.IsNullOrWhitespace</ReplaceComment>
    <ReplacePattern>string.IsNullOrWhitespace($name$)</ReplacePattern>
    <SearchPattern>Object.ReferenceEquals(null, $name$) || $name$.Trim().Length == 0</SearchPattern>
    <Params />
    <Placeholders>
      <IdentifierPlaceholder Name="name" Type="" ExactType="False" RegEx="" CaseSensitive="True" />
    </Placeholders>
  </Pattern>
</CustomPatterns>


4. Try to search through the catalog, notice that it does indeed find the usage
5. Try to click Replace, notice that the file stays unchanged
6. Save and exit back to the editor, notice that the only quick-fix hints you can apparently get on the line is to remove the redundant identifier (Object.) or convert the 0 to hex.

This is RTM of both ReSharper 5.0 and Visual Studio 2010, running on Windows 7 64-bit, all patched up.

I've tried with no other addins or extensions installed, makes no difference. The addins I have installed normally are TestDriven, NDepend, VisualSVN, all updated to latest as of today.


I've attached a file with both a sample solution and the pattern.xml file.

Here's what I see when do this on my own computer:
http://www.screencast.com/users/lassevk/folders/Default/media/565ff417-be23-407e-9ca9-36676ee86066



Attachment(s):
SearchPatternTest.zip
0
Comment actions Permalink

This is a bug, thanks for reporting it. Test case was very helpful.
It's fixed and will be included in R# 5.1

0

Please sign in to leave a comment.