/src/Tests/Analyzers.Tests/RCS1221UsePatternMatchingInsteadOfAsAndNullCheckTests.cs

https://github.com/JosefPihrt/Roslynator · C# · 341 lines · 323 code · 17 blank · 1 comment · 0 complexity · c910dce7f8be866793225cccdf46c081 MD5 · raw file

  1. // Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  2. using System.Threading.Tasks;
  3. using Microsoft.CodeAnalysis;
  4. using Microsoft.CodeAnalysis.CodeFixes;
  5. using Microsoft.CodeAnalysis.Diagnostics;
  6. using Roslynator.CSharp.Analysis.UsePatternMatching;
  7. using Roslynator.CSharp.CodeFixes;
  8. using Roslynator.CSharp.Testing;
  9. using Xunit;
  10. namespace Roslynator.CSharp.Analysis.Tests
  11. {
  12. public class RCS1221UsePatternMatchingInsteadOfAsAndNullCheckTests : AbstractCSharpFixVerifier
  13. {
  14. public override DiagnosticDescriptor Descriptor { get; } = DiagnosticDescriptors.UsePatternMatchingInsteadOfAsAndNullCheck;
  15. public override DiagnosticAnalyzer Analyzer { get; } = new UsePatternMatchingInsteadOfAsAndNullCheckAnalyzer();
  16. public override CodeFixProvider FixProvider { get; } = new UsePatternMatchingInsteadOfAsAndNullCheckCodeFixProvider();
  17. [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UsePatternMatchingInsteadOfAsAndNullCheck)]
  18. public async Task Test_EqualsToNull()
  19. {
  20. await VerifyDiagnosticAndFixAsync(@"
  21. class C
  22. {
  23. void M()
  24. {
  25. object x = null;
  26. [|var s = x as string;|]
  27. if (s == null)
  28. {
  29. return;
  30. }
  31. }
  32. }
  33. ", @"
  34. class C
  35. {
  36. void M()
  37. {
  38. object x = null;
  39. if (!(x is string s))
  40. {
  41. return;
  42. }
  43. }
  44. }
  45. ");
  46. }
  47. [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UsePatternMatchingInsteadOfAsAndNullCheck)]
  48. public async Task Test_EqualsToNull_ExplicitType()
  49. {
  50. await VerifyDiagnosticAndFixAsync(@"
  51. class C
  52. {
  53. void M()
  54. {
  55. object x = null;
  56. [|string s = x as string;|]
  57. if (s == null)
  58. return;
  59. }
  60. }
  61. ", @"
  62. class C
  63. {
  64. void M()
  65. {
  66. object x = null;
  67. if (!(x is string s))
  68. return;
  69. }
  70. }
  71. ");
  72. }
  73. [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UsePatternMatchingInsteadOfAsAndNullCheck)]
  74. public async Task Test_EqualsToNull_ReturnVariable()
  75. {
  76. await VerifyDiagnosticAndFixAsync(@"
  77. class C
  78. {
  79. string M()
  80. {
  81. object x = null;
  82. [|var s = x as string;|]
  83. if (s == null)
  84. {
  85. return s;
  86. }
  87. return s;
  88. }
  89. }
  90. ", @"
  91. class C
  92. {
  93. string M()
  94. {
  95. object x = null;
  96. if (!(x is string s))
  97. {
  98. return null;
  99. }
  100. return s;
  101. }
  102. }
  103. ");
  104. }
  105. [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UsePatternMatchingInsteadOfAsAndNullCheck)]
  106. public async Task Test_IsNull()
  107. {
  108. await VerifyDiagnosticAndFixAsync(@"
  109. class C
  110. {
  111. void M()
  112. {
  113. object x = null;
  114. [|var s = x as string;|]
  115. if (s is null)
  116. {
  117. return;
  118. }
  119. }
  120. }
  121. ", @"
  122. class C
  123. {
  124. void M()
  125. {
  126. object x = null;
  127. if (!(x is string s))
  128. {
  129. return;
  130. }
  131. }
  132. }
  133. ");
  134. }
  135. [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UsePatternMatchingInsteadOfAsAndNullCheck)]
  136. public async Task TestNoDiagnostic_MultipleLocalDeclarations()
  137. {
  138. await VerifyNoDiagnosticAsync(@"
  139. class C
  140. {
  141. void M()
  142. {
  143. object x = null;
  144. string s = x as string, y = x as string;
  145. if (s == null)
  146. {
  147. return;
  148. }
  149. }
  150. }
  151. ");
  152. }
  153. [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UsePatternMatchingInsteadOfAsAndNullCheck)]
  154. public async Task TestNoDiagnostic_NotSimpleIf()
  155. {
  156. await VerifyNoDiagnosticAsync(@"
  157. class C
  158. {
  159. void M()
  160. {
  161. object x = null;
  162. var s = x as string;
  163. if (s == null)
  164. {
  165. return;
  166. }
  167. else
  168. {
  169. }
  170. }
  171. }
  172. ");
  173. }
  174. [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UsePatternMatchingInsteadOfAsAndNullCheck)]
  175. public async Task TestNoDiagnostic_DoesNotContainJumpStatement()
  176. {
  177. await VerifyNoDiagnosticAsync(@"
  178. class C
  179. {
  180. void M()
  181. {
  182. object x = null;
  183. var s = x as string;
  184. if (s == null)
  185. {
  186. M();
  187. }
  188. }
  189. }
  190. ");
  191. }
  192. [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UsePatternMatchingInsteadOfAsAndNullCheck)]
  193. public async Task TestNoDiagnostic_NotEqualsToNull()
  194. {
  195. await VerifyNoDiagnosticAsync(@"
  196. class C
  197. {
  198. void M()
  199. {
  200. object x = null;
  201. var s = x as string;
  202. if (s != null)
  203. {
  204. return;
  205. }
  206. }
  207. }
  208. ");
  209. }
  210. [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UsePatternMatchingInsteadOfAsAndNullCheck)]
  211. public async Task TestNoDiagnostic_OtherVariableCheckedForNull()
  212. {
  213. await VerifyNoDiagnosticAsync(@"
  214. class C
  215. {
  216. void M()
  217. {
  218. object x = null;
  219. string s = null;
  220. var s2 = x as string;
  221. if (s == null)
  222. {
  223. return;
  224. }
  225. }
  226. }
  227. ");
  228. }
  229. [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UsePatternMatchingInsteadOfAsAndNullCheck)]
  230. public async Task TestNoDiagnostic_TypesDoNotEqual()
  231. {
  232. await VerifyNoDiagnosticAsync(@"
  233. class C
  234. {
  235. void M()
  236. {
  237. object x = null;
  238. object o = x as string;
  239. if (o == null)
  240. {
  241. return;
  242. }
  243. }
  244. }
  245. ");
  246. }
  247. [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UsePatternMatchingInsteadOfAsAndNullCheck)]
  248. public async Task TestNoDiagnostic_Directive()
  249. {
  250. await VerifyNoDiagnosticAsync(@"
  251. class C
  252. {
  253. void M()
  254. {
  255. object x = null;
  256. #region
  257. var s = x as string;
  258. #endregion
  259. if (s == null)
  260. {
  261. return;
  262. }
  263. }
  264. }
  265. ");
  266. }
  267. [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UsePatternMatchingInsteadOfAsAndNullCheck)]
  268. public async Task TestNoDiagnostic_NullableType()
  269. {
  270. await VerifyNoDiagnosticAsync(@"
  271. class C
  272. {
  273. void M()
  274. {
  275. object x = null;
  276. var y = x as int?;
  277. if (y == null)
  278. {
  279. }
  280. }
  281. }
  282. ");
  283. }
  284. [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UsePatternMatchingInsteadOfAsAndNullCheck)]
  285. public async Task TestNoDiagnostic_LanguageVersion()
  286. {
  287. await VerifyNoDiagnosticAsync(@"
  288. class C
  289. {
  290. void M()
  291. {
  292. object x = null;
  293. var s = x as string;
  294. if (s == null)
  295. {
  296. return;
  297. }
  298. }
  299. }
  300. ", options: CSharpCodeVerificationOptions.Default_CSharp6);
  301. }
  302. }
  303. }