43 | /// [CanBeNull] object Test() => null;
44 | ///
45 | /// void UseTest() {
46 | /// var p = Test();
47 | /// var s = p.ToString(); // Warning: Possible 'System.NullReferenceException'
48 | /// }
49 | ///
61 | /// [NotNull] object Foo() {
62 | /// return null; // Warning: Possible 'null' assignment
63 | /// }
64 | ///
78 | /// public void Foo([ItemNotNull]List<string> books)
79 | /// {
80 | /// foreach (var book in books) {
81 | /// if (book != null) // Warning: Expression is always true
82 | /// Console.WriteLine(book.ToUpper());
83 | /// }
84 | /// }
85 | ///
98 | /// public void Foo([ItemCanBeNull]List<string> books)
99 | /// {
100 | /// foreach (var book in books)
101 | /// {
102 | /// // Warning: Possible 'System.NullReferenceException'
103 | /// Console.WriteLine(book.ToUpper());
104 | /// }
105 | /// }
106 | ///
119 | /// [StringFormatMethod("message")]
120 | /// void ShowError(string message, params object[] args) { /* do something */ }
121 | ///
122 | /// void Foo() {
123 | /// ShowError("Failed: {0}"); // Warning: Non-existing argument in format string
124 | /// }
125 | ///
149 | /// namespace TestNamespace
150 | /// {
151 | /// public class Constants
152 | /// {
153 | /// public static int INT_CONST = 1;
154 | /// public const string STRING_CONST = "1";
155 | /// }
156 | ///
157 | /// public class Class1
158 | /// {
159 | /// [ValueProvider("TestNamespace.Constants")] public int myField;
160 | /// public void Foo([ValueProvider("TestNamespace.Constants")] string str) { }
161 | ///
162 | /// public void Test()
163 | /// {
164 | /// Foo(/*try completion here*/);//
165 | /// myField = /*try completion here*/
166 | /// }
167 | /// }
168 | /// }
169 | ///
190 | /// void Foo([ValueRange(0, 100)] int value) {
191 | /// if (value == -1) { // Warning: Expression is always 'false'
192 | /// ...
193 | /// }
194 | /// }
195 | ///
233 | /// void Foo([NonNegativeValue] int value) {
234 | /// if (value == -1) { // Warning: Expression is always 'false'
235 | /// ...
236 | /// }
237 | /// }
238 | ///
251 | /// void Foo(string param) {
252 | /// if (param == null)
253 | /// throw new ArgumentNullException("par"); // Warning: Cannot resolve symbol
254 | /// }
255 | ///
276 | /// public class Foo : INotifyPropertyChanged {
277 | /// public event PropertyChangedEventHandler PropertyChanged;
278 | ///
279 | /// [NotifyPropertyChangedInvocator]
280 | /// protected virtual void NotifyChanged(string propertyName) { ... }
281 | ///
282 | /// string _name;
283 | ///
284 | /// public string Name {
285 | /// get { return _name; }
286 | /// set { _name = value; NotifyChanged("LastName"); /* Warning */ }
287 | /// }
288 | /// }
289 | ///
290 | /// Examples of generated notifications:
291 | /// Function Definition Table syntax:
316 | ///
333 | /// [ContractAnnotation("=> halt")]
334 | /// public void TerminationMethod()
335 | ///
337 | /// [ContractAnnotation("null <= param:null")] // reverse condition syntax
338 | /// public string GetName(string surname)
339 | ///
341 | /// [ContractAnnotation("s:null => true")]
342 | /// public bool IsNullOrEmpty(string s) // string.IsNullOrEmpty()
343 | ///
345 | /// // A method that returns null if the parameter is null,
346 | /// // and not null if the parameter is not null
347 | /// [ContractAnnotation("null => null; notnull => notnull")]
348 | /// public object Transform(object data)
349 | ///
351 | /// [ContractAnnotation("=> true, result: notnull; => false, result: null")]
352 | /// public bool TryParse(string s, out Person result)
353 | ///
377 | /// [LocalizationRequiredAttribute(true)]
378 | /// class Foo {
379 | /// string str = "my string"; // Warning: Localizable string
380 | /// }
381 | ///
403 | /// [CannotApplyEqualityOperator]
404 | /// class NoEquality { }
405 | ///
406 | /// class UsesNoEquality {
407 | /// void Test() {
408 | /// var ca1 = new NoEquality();
409 | /// var ca2 = new NoEquality();
410 | /// if (ca1 != null) { // OK
411 | /// bool condition = ca1 == ca2; // Warning
412 | /// }
413 | /// }
414 | /// }
415 | ///
425 | /// [BaseTypeRequired(typeof(IComponent)] // Specify requirement
426 | /// class ComponentAttribute : Attribute { }
427 | ///
428 | /// [Component] // ComponentAttribute requires implementing IComponent interface
429 | /// class MyComponent : IComponent { }
430 | ///
573 | /// [Pure] int Multiply(int x, int y) => x * y;
574 | ///
575 | /// void M() {
576 | /// Multiply(123, 42); // Warning: Return value of pure method is not used
577 | /// }
578 | ///
[MustUseReturnValue("Use the return value to...")]
.
593 | ///
614 | /// class Foo {
615 | /// [ProvidesContext] IBarService _barService = ...;
616 | ///
617 | /// void ProcessNode(INode node) {
618 | /// DoSomething(node, node.GetGlobalServices().Bar);
619 | /// // ^ Warning: use value of '_barService' field
620 | /// }
621 | /// }
622 | ///
662 | /// [SourceTemplate]
663 | /// public static void forEach<T>(this IEnumerable<T> xs) {
664 | /// foreach (var x in xs) {
665 | /// //$ $END$
666 | /// }
667 | /// }
668 | ///
669 | ///
686 | /// [SourceTemplate, Macro(Target = "item", Expression = "suggestVariableName()")]
687 | /// public static void forEach<T>(this IEnumerable<T> collection) {
688 | /// foreach (var item in collection) {
689 | /// //$ $END$
690 | /// }
691 | /// }
692 | ///
693 | /// Applying the attribute on a template method parameter:
694 | ///
695 | /// [SourceTemplate]
696 | /// public static void something(this Entity x, [Macro(Expression = "guid()", Editable = -1)] string newguid) {
697 | /// /*$ var $x$Id = "$newguid$" + x.ToString();
698 | /// x.DoSomething($x$Id); */
699 | /// }
700 | ///
701 | ///
951 | /// [ActionName("Foo")]
952 | /// public ActionResult Login(string returnUrl) {
953 | /// ViewBag.ReturnUrl = Url.Action("Foo"); // OK
954 | /// return RedirectToAction("Bar"); // Error: Cannot resolve action
955 | /// }
956 | ///
1005 | /// public class MyStringCollection : List<string>
1006 | /// {
1007 | /// [CollectionAccess(CollectionAccessType.Read)]
1008 | /// public string GetFirstString()
1009 | /// {
1010 | /// return this.ElementAt(0);
1011 | /// }
1012 | /// }
1013 | /// class Test
1014 | /// {
1015 | /// public void Foo()
1016 | /// {
1017 | /// // Warning: Contents of the collection is never updated
1018 | /// var col = new MyStringCollection();
1019 | /// string x = col.GetFirstString();
1020 | /// }
1021 | /// }
1022 | ///
1117 | /// static void ThrowIfNull<T>([NoEnumeration] T v, string n) where T : class
1118 | /// {
1119 | /// // custom check for null but no enumeration
1120 | /// }
1121 | ///
1122 | /// void Foo(IEnumerable<string> values)
1123 | /// {
1124 | /// ThrowIfNull(values, nameof(values));
1125 | /// var x = values.ToList(); // No warnings about multiple enumeration
1126 | /// }
1127 | ///