rspec/rules/S1172/csharp/rule.adoc

88 lines
2.1 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2020-06-30 12:47:33 +02:00
Unused parameters are misleading. Whatever the values passed to such parameters, the behavior will be the same.
2021-02-02 15:02:10 +01:00
This rule raises an issue when a ``++private++`` method or constructor of a class/struct takes a parameter without using it.
2020-06-30 12:47:33 +02:00
=== Noncompliant code example
2020-06-30 12:47:33 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:47:33 +02:00
----
private void DoSomething(int a, int b) // "b" is unused
{
2020-06-30 12:47:33 +02:00
Compute(a);
}
private void DoSomething2(int a) // value of "a" is unused
{
2020-06-30 12:47:33 +02:00
a = 10;
Compute(a);
}
----
=== Compliant solution
2020-06-30 12:47:33 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:47:33 +02:00
----
private void DoSomething(int a)
{
2020-06-30 12:47:33 +02:00
Compute(a);
}
private void DoSomething2()
{
2020-06-30 12:47:33 +02:00
var a = 10;
Compute(a);
}
----
=== Exceptions
2020-06-30 12:47:33 +02:00
This rule doesn't raise any issue in the following contexts:
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
* The ``++this++`` parameter of extension methods.
2020-06-30 12:47:33 +02:00
* Methods decorated with attributes.
* Empty methods.
2021-01-27 13:42:22 +01:00
* Methods which only throw ``++NotImplementedException++``.
2020-06-30 12:47:33 +02:00
* Main methods.
2021-01-27 13:42:22 +01:00
* ``++virtual++``, ``++override++`` methods.
2020-06-30 12:47:33 +02:00
* interface implementations.
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
* Remove this unused method parameter "{0}".
* Remove this parameter "{0}", whose value is ignored in the method.
'''
== Comments And Links
(visible only on this page)
=== on 6 Mar 2017, 17:55:59 Amaury Levé wrote:
\[~freddy.mallet] Shall we ignore all empty methods or only public ones? If you say all of them, shall we also ignore empty Ctor?
About ``++static void Main(string[] args)++`` do you mean we also ignore the argument even if the method is not empty?
=== on 7 Mar 2017, 10:08:34 Jean-Christophe Collet wrote:
My suggestions:
* Ignore all empty methods including constructors (they should be flagged by another issue anyway)
* Yes, since Main(string[] args) is a 'non-negotiable' signature, we should ignore the case where 'args' is not used.
=== on 7 Jan 2019, 16:44:49 Nicolas Harraudeau wrote:
The new description matches the existing implementation. It just explains more precisely what cases raise an issue.
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]