Types in search pattern expressions not working

I'm having a hard time figuring out how to get my expression type right. I want to add a custom pattern that flags usage of ElementAt() on IList types as an error. So I am trying this:

   Pattern = $e1$.ElementAt($e2$)
   Replace = $e1$[$e2$]

Where e1 is an expression of type System.Collections.Generic.IList<T> + "or derived type" and e2 is just an expression.

Yet with this simple expression it does not match:

   var x = new[] { 1, 2, 3 };
   var y = x.ElementAt(1);

I would expect that call to ElementAt to get flagged as an error with a suggestion to switch to the indexing operator.

What am I doing wrong?

7 comments

Hello Scott,

The following pattern works fine for me in this case:



Let me know if this helps. Thank you!

Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

I'm having a hard time figuring out how to get my expression type
right. I want to add a custom pattern that flags usage of ElementAt()
on IList types as an error. So I am trying this:

Pattern = $e1$.ElementAt($e2$)
Replace = $e1$[$e2$]
Where e1 is an expression of type System.Collections.Generic.IList<T>
+ "or derived type" and e2 is just an expression.

Yet with this simple expression it does not match:

var x = new[] { 1, 2, 3 };
var y = x.ElementAt(1);
I would expect that call to ElementAt to get flagged as an error with
a suggestion to switch to the indexing operator.

What am I doing wrong?

---
Original message URL:
http://devnet.jetbrains.net/message/5312410#5312410



0

Which case? Did you forget to paste something maybe?

0

Hello Scott,

I mean that the pattern above finds the second statement in

var x = new[] { 1, 2, 3 };
var y = x.ElementAt(1);

for me just fine. Thank you!

Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

Which case? Did you forget to paste something maybe?

---
Original message URL:
http://devnet.jetbrains.net/message/5312501#5312501



0

Did you try constraining it to System.Collections.Generic.IList<T>?

I opened up a new test project and if I set 'e1' to type System.Collections.IList it works as I would expect (and is actually what I want in this case, now that I think about it).

But if the type is System.Collections.Generic.IList<T> it does not work. Something wrong with generic types perhaps?

0

Hello Scott,

Yes, there's a known problem with generics, so if you'll constrain 'e1' to
IList
will find that usage as well, but it won't work on non-integer arrays and
collections. Thank you!

Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

Did you try constraining it to System.Collections.Generic.IList<T>?

I opened up a new test project and if I set 'e1' to type
System.Collections.IList it works as I would expect (and is actually
what I want in this case, now that I think about it).

But if the type is System.Collections.Generic.IList<T> it does not
work. Something wrong with generic types perhaps?

---
Original message URL:
http://devnet.jetbrains.net/message/5312819#5312819



0

In our codebase a lot of people are still learning LINQ and the enumerable operators and making lots of usage mistakes. I'm trying to add many rules to catch these as warnings. So obviously it is difficult when I can't constrain types to IEnumerable<T> and so on.

Is there a bug I can vote for on this? :)

0

Maybe one of these?
http://youtrack.jetbrains.net/issue/RSRP-182435 "Structural search and replace: Can't replace generic type constraints."
http://youtrack.jetbrains.net/issue/RSRP-261552 "SSR Pattern not workiing with Generics"

0

Please sign in to leave a comment.