question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Diagnostic for argument constraint with non-nullable type instead of nullable type

See original GitHub issue

Split from #1005. Inspired by #660.

Assuming you have this interface:

public interface IFoo
{
    int Bar(int? x);
}

And a call configuration like this:

A.CallTo(() => foo.Bar(A<int>._)).Returns(42);

It’s easy to miss the fact that the constraint won’t match when the argument is null (it used to throw a NRE, which at least indicated something was wrong, but now it silently rejects the null argument). It’s especially confusing because when you write A<int>._, you usually mean “match anything”. The analyzer could detect this and emit a warning.

(I wish we could omit the type and just say A.CallTo(() => foo.Bar(A._), which is more concise and easier to get right. Java’s type inference is a little better in that regard. This syntax could also be achieved if we had a “bottom type” in C#.)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
thomaslevesquecommented, Jun 20, 2017

False positives are the worst of all situations, so if the That analysis might overreport warnings, I would leave it out. It’s easy to add new cases to the analyzer later.

A fix for Ignored (and _) sounds grand!

The thing is, this analyzer is bound to cause false positives, because you can intentionally write A<int>._ where a int? is expected. This is why I want to provide a code-fix to easily change it to something that is equivalent and more obviously correct. But since I can’t do that for A<T>.That..., the user would have to suppress the warning if it was intentional.

So I think I’ll just restrict the warning to Ignored and _.

0reactions
blairconradcommented, Jul 27, 2017

This change has been released as part of FakeItEasy 4.0.0.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Constraints on type parameters - C# Programming Guide
The type argument must be a reference type, either nullable or non-nullable. This constraint applies also to any class, interface, delegate, ...
Read more >
The type 'string' must be a non-nullable type in order to use ...
The Nullable<T> type requires that T is a non-nullable value type, for example int or DateTime . Reference types like string can already...
Read more >
Regression with Nullable<T> and a generic type constraint ...
As a workaround you can write Nullable<TResult> in the override declaration.
Read more >
C# 8 non-nullable references and the Try pattern
The challenge you'll face is that when using nullable reference types (NRT), the compiler will treat Foo<T> as non-nullable. But try and change ......
Read more >
Improving non-nullable reference types handling
The main thing that started getting on my nerves was diagnostic CS8618: Non-nullable field is uninitialized. Consider declaring as nullable.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found