41 | /// [CanBeNull] object Test() => null;
42 | ///
43 | /// void UseTest() {
44 | /// var p = Test();
45 | /// var s = p.ToString(); // Warning: Possible 'System.NullReferenceException'
46 | /// }
47 | ///
58 | /// [NotNull] object Foo() {
59 | /// return null; // Warning: Possible 'null' assignment
60 | /// }
61 | ///
74 | /// public void Foo([ItemNotNull]List<string> books)
75 | /// {
76 | /// foreach (var book in books) {
77 | /// if (book != null) // Warning: Expression is always true
78 | /// Console.WriteLine(book.ToUpper());
79 | /// }
80 | /// }
81 | ///
93 | /// public void Foo([ItemCanBeNull]List<string> books)
94 | /// {
95 | /// foreach (var book in books)
96 | /// {
97 | /// // Warning: Possible 'System.NullReferenceException'
98 | /// Console.WriteLine(book.ToUpper());
99 | /// }
100 | /// }
101 | ///
113 | /// [StringFormatMethod("message")]
114 | /// void ShowError(string message, params object[] args) { /* do something */ }
115 | ///
116 | /// void Foo() {
117 | /// ShowError("Failed: {0}"); // Warning: Non-existing argument in format string
118 | /// }
119 | ///
142 | /// namespace TestNamespace
143 | /// {
144 | /// public class Constants
145 | /// {
146 | /// public static int INT_CONST = 1;
147 | /// public const string STRING_CONST = "1";
148 | /// }
149 | ///
150 | /// public class Class1
151 | /// {
152 | /// [ValueProvider("TestNamespace.Constants")] public int myField;
153 | /// public void Foo([ValueProvider("TestNamespace.Constants")] string str) { }
154 | ///
155 | /// public void Test()
156 | /// {
157 | /// Foo(/*try completion here*/);//
158 | /// myField = /*try completion here*/
159 | /// }
160 | /// }
161 | /// }
162 | ///
182 | /// void Foo([ValueRange(0, 100)] int value) {
183 | /// if (value == -1) { // Warning: Expression is always 'false'
184 | /// ...
185 | /// }
186 | /// }
187 | ///
224 | /// void Foo([NonNegativeValue] int value) {
225 | /// if (value == -1) { // Warning: Expression is always 'false'
226 | /// ...
227 | /// }
228 | /// }
229 | ///
241 | /// void Foo(string param) {
242 | /// if (param == null)
243 | /// throw new ArgumentNullException("par"); // Warning: Cannot resolve symbol
244 | /// }
245 | ///
265 | /// public class Foo : INotifyPropertyChanged {
266 | /// public event PropertyChangedEventHandler PropertyChanged;
267 | ///
268 | /// [NotifyPropertyChangedInvocator]
269 | /// protected virtual void NotifyChanged(string propertyName) { ... }
270 | ///
271 | /// string _name;
272 | ///
273 | /// public string Name {
274 | /// get { return _name; }
275 | /// set { _name = value; NotifyChanged("LastName"); /* Warning */ }
276 | /// }
277 | /// }
278 | ///
279 | /// Examples of generated notifications:
280 | /// Function Definition Table syntax:
304 | ///
321 | /// [ContractAnnotation("=> halt")]
322 | /// public void TerminationMethod()
323 | ///
325 | /// [ContractAnnotation("null <= param:null")] // reverse condition syntax
326 | /// public string GetName(string surname)
327 | ///
329 | /// [ContractAnnotation("s:null => true")]
330 | /// public bool IsNullOrEmpty(string s) // string.IsNullOrEmpty()
331 | ///
333 | /// // A method that returns null if the parameter is null,
334 | /// // and not null if the parameter is not null
335 | /// [ContractAnnotation("null => null; notnull => notnull")]
336 | /// public object Transform(object data)
337 | ///
339 | /// [ContractAnnotation("=> true, result: notnull; => false, result: null")]
340 | /// public bool TryParse(string s, out Person result)
341 | ///
364 | /// [LocalizationRequiredAttribute(true)]
365 | /// class Foo {
366 | /// string str = "my string"; // Warning: Localizable string
367 | /// }
368 | ///
389 | /// [CannotApplyEqualityOperator]
390 | /// class NoEquality { }
391 | ///
392 | /// class UsesNoEquality {
393 | /// void Test() {
394 | /// var ca1 = new NoEquality();
395 | /// var ca2 = new NoEquality();
396 | /// if (ca1 != null) { // OK
397 | /// bool condition = ca1 == ca2; // Warning
398 | /// }
399 | /// }
400 | /// }
401 | ///
410 | /// [BaseTypeRequired(typeof(IComponent)] // Specify requirement
411 | /// class ComponentAttribute : Attribute { }
412 | ///
413 | /// [Component] // ComponentAttribute requires implementing IComponent interface
414 | /// class MyComponent : IComponent { }
415 | ///
553 | /// [Pure] int Multiply(int x, int y) => x * y;
554 | ///
555 | /// void M() {
556 | /// Multiply(123, 42); // Warning: Return value of pure method is not used
557 | /// }
558 | /// [MustUseReturnValue("Use the return value to...")].
572 | ///
592 | /// class Foo {
593 | /// [ProvidesContext] IBarService _barService = ...;
594 | ///
595 | /// void ProcessNode(INode node) {
596 | /// DoSomething(node, node.GetGlobalServices().Bar);
597 | /// // ^ Warning: use value of '_barService' field
598 | /// }
599 | /// }
600 | ///
638 | /// [SourceTemplate]
639 | /// public static void forEach<T>(this IEnumerable<T> xs) {
640 | /// foreach (var x in xs) {
641 | /// //$ $END$
642 | /// }
643 | /// }
644 | ///
645 | ///
661 | /// [SourceTemplate, Macro(Target = "item", Expression = "suggestVariableName()")]
662 | /// public static void forEach<T>(this IEnumerable<T> collection) {
663 | /// foreach (var item in collection) {
664 | /// //$ $END$
665 | /// }
666 | /// }
667 | ///
668 | /// Applying the attribute on a template method parameter:
669 | ///
670 | /// [SourceTemplate]
671 | /// public static void something(this Entity x, [Macro(Expression = "guid()", Editable = -1)] string newguid) {
672 | /// /*$ var $x$Id = "$newguid$" + x.ToString();
673 | /// x.DoSomething($x$Id); */
674 | /// }
675 | ///
676 | ///
906 | /// [ActionName("Foo")]
907 | /// public ActionResult Login(string returnUrl) {
908 | /// ViewBag.ReturnUrl = Url.Action("Foo"); // OK
909 | /// return RedirectToAction("Bar"); // Error: Cannot resolve action
910 | /// }
911 | ///
956 | /// public class MyStringCollection : List<string>
957 | /// {
958 | /// [CollectionAccess(CollectionAccessType.Read)]
959 | /// public string GetFirstString()
960 | /// {
961 | /// return this.ElementAt(0);
962 | /// }
963 | /// }
964 | /// class Test
965 | /// {
966 | /// public void Foo()
967 | /// {
968 | /// // Warning: Contents of the collection is never updated
969 | /// var col = new MyStringCollection();
970 | /// string x = col.GetFirstString();
971 | /// }
972 | /// }
973 | ///
1063 | /// static void ThrowIfNull<T>([NoEnumeration] T v, string n) where T : class
1064 | /// {
1065 | /// // custom check for null but no enumeration
1066 | /// }
1067 | ///
1068 | /// void Foo(IEnumerable<string> values)
1069 | /// {
1070 | /// ThrowIfNull(values, nameof(values));
1071 | /// var x = values.ToList(); // No warnings about multiple enumeration
1072 | /// }
1073 | ///