CA1823 False Positive
-
Tuesday, January 17, 2006 3:39 PMIt's easy to see why this false positive occurs, but it's still annoying!
Consider the following code (from a plain-vanilla console app):
class Program
{
static void Main(string[] args)
{
if (args.Length == 1)
throw new InvalidOperationException(TEST + "X");
}
const string TEST = "X";
}
This will throw up warning CA1823, presumably because the compiler is concatenating TEST with "X" at compile time, therefore the variable TEST never appears to be used...
All Replies
-
Tuesday, January 17, 2006 3:49 PM
This false positive turns up a lot. It's interesting how many const strings out that are used exclusively as a substring within another pattern. 8)
We will either clear up this noise or disable the check in the next (post 1.35) update. Sorry for the inconvenience and thanks for the report.
-
Tuesday, January 17, 2006 3:52 PMThanks for the quick response. This doesn't happen for us very much, because we internationalize all our strings except for exception messages, so it's not a big deal.
I get around the few occurances that we get by using string.Format().
-
Tuesday, January 17, 2006 3:58 PM
That's a fine approach until we ship our rule that states 'don't use string.format, an expensive method, to perform simple string concatenations'. 8)
I guess what this means is we'll need to clear up that false positive before we release the new check.
-
Wednesday, March 08, 2006 11:18 AMWhen is the next version of FXCOP expected?
Will it be tied to the service pack for Visual Studio 2005?
I have encounted this error while writing a class library for NUNIT tests using VB.NET within Visual Studio 2005 Professional. The constant replaces some magic strings within the data create routines and data return asserts. If the comments which FXCOP asserts are unsed are commented out, then the code will not compile. -
Wednesday, March 08, 2006 11:55 AMThe specifc constant that does not register as used in an Assert.Less in code of which the following shows an anaomised example of the code in use.
Assert.Less(dVar1, DEFAULT_NUMERIC1)
However other constants used in a similar manner of assign and test does not get an error.
Assert.AreEqual(DEFAULT_STRING1, sVar1)
I have had to turn CA1303 off, as I am using test strings that have to go in "as is" and return "as is". CA1822 is also turned off. -
Friday, March 10, 2006 2:50 PMWill Don't use string.format, an expensive method, to perform simple string concatenations be both part of FxCop an Visual Studio Team Suite Code Analysis?
-
Tuesday, April 18, 2006 6:50 PM
What is the update time-frame for this?
We have a bunch of const decimal declarations that suffer from the same problem... i.e. it appears that during the CA process that the compiler does the in-line substitutions of the consts, then performs the CA and determines that the consts are never used (since they have already been subbed for in-line)
-
Tuesday, April 18, 2006 8:21 PMModeratorThis has made it into the external release of FxCop 1.35 and should also be fixed in Visual Studio 2005 SP1.
-
Wednesday, April 19, 2006 11:09 AMThank you.
I have downloaded FxCop 1.35 from http://www.gotdotnet.com/team/fxcop/ and installed it. However how do I know which version of FXCOP, visual studio runs? I have been to VS2005's help about page, but the version of FXCOP is not reported. -
Wednesday, April 19, 2006 1:02 PMModerator
The version of FxCop that ships with Visual Studio does not correspond with an external release.
Visual Studio SP1 will not ship until later this year.

