{"record":{"id":"877e1cfe5f4f592f","repo":"jstedfast/MailKit","slug":"no-flags-specified","errorCode":null,"errorMessage":"No flags specified.","messagePattern":"No flags specified\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/Search/SearchQuery.cs","lineNumber":418,"sourceCode":"\t\tpublic static SearchQuery HasFlags (MessageFlags flags)\n\t\t{\n\t\t\tvar list = new List<SearchQuery> ();\n\n\t\t\tif ((flags & MessageFlags.Seen) != 0)\n\t\t\t\tlist.Add (Seen);\n\t\t\tif ((flags & MessageFlags.Answered) != 0)\n\t\t\t\tlist.Add (Answered);\n\t\t\tif ((flags & MessageFlags.Flagged) != 0)\n\t\t\t\tlist.Add (Flagged);\n\t\t\tif ((flags & MessageFlags.Deleted) != 0)\n\t\t\t\tlist.Add (Deleted);\n\t\t\tif ((flags & MessageFlags.Draft) != 0)\n\t\t\t\tlist.Add (Draft);\n\t\t\tif ((flags & MessageFlags.Recent) != 0)\n\t\t\t\tlist.Add (Recent);\n\n\t\t\tif (list.Count == 0)\n\t\t\t\tthrow new ArgumentException (\"No flags specified.\", nameof (flags));\n\n\t\t\tvar query = list[0];\n\t\t\tfor (int i = 1; i < list.Count; i++)\n\t\t\t\tquery = query.And (list[i]);\n\n\t\t\treturn query;\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Match messages that do not have any of the specified flags set.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// <para>Matches messages that do not have any of the specified flags set.</para>\n\t\t/// <note type=\"note\">Maps each flag to the corresponding search key (<c>UNANSWERED</c>, <c>UNDELETED</c>, <c>UNDRAFT</c>, <c>UNFLAGGED</c>,\n\t\t/// <c>OLD</c> or <c>UNSEEN</c>) as defined in <a href=\"https://datatracker.ietf.org/doc/html/rfc3501#section-6.4.4\">rfc3501</a>.</note>\n\t\t/// </remarks>\n\t\t/// <returns>A <see cref=\"SearchQuery\"/>.</returns>\n\t\t/// <param name=\"flags\">The message flags.</param>","sourceCodeStart":400,"sourceCodeEnd":436,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Search/SearchQuery.cs#L400-L436","documentation":"SearchQuery.HasFlags builds an IMAP FLAGS search query by AND-ing one sub-query per MessageFlags bit set in the argument. If no recognized bits are set, there is no query to build, so it throws ArgumentException. This guards against silently producing an empty/invalid search term.","triggerScenarios":"Calling SearchQuery.HasFlags with a MessageFlags value of 0 (e.g. (MessageFlags)0, a default-struct value, or an enum cast that lost all bits).","commonSituations":"Building search criteria programmatically where flags were accumulated in a variable that was never assigned; casting an int/enum from user config that maps to no MessageFlags bit; default(MessageFlags) passed by mistake.","solutions":["Ensure at least one MessageFlags bit (e.g. MessageFlags.Seen, Draft, Recent, Flagged, Answered, Deleted) is set before calling HasFlags.","Check the value with `if (flags != MessageFlags.None) query = SearchQuery.HasFlags(flags);` and otherwise skip the flag criterion.","Verify any cast/calculation producing the MessageFlags value actually maps intended bits onto the MailKit enum."],"exampleFix":"// before\nvar query = SearchQuery.HasFlags(unreadFlags); // unreadFlags == 0\n// after\nvar query = unreadFlags != MessageFlags.None\n    ? SearchQuery.HasFlags(unreadFlags)\n    : SearchQuery.All;","handlingStrategy":"validation","validationCode":"if (flags == MessageFlags.None)\n    throw new ArgumentException(\"At least one MessageFlags bit must be set.\", nameof(flags));\nvar query = SearchQuery.HasFlags(flags);","typeGuard":"bool HasValidFlags(MessageFlags flags) => flags != MessageFlags.None;","tryCatchPattern":"try { query = SearchQuery.HasFlags(flags); }\ncatch (ArgumentException ex) { log.Warn(ex.Message); query = SearchQuery.All; }","preventionTips":["Never pass default(MessageFlags); initialize flag accumulators to a meaningful combination or None and branch on it.","Wrap raw casts to MessageFlags in a parse function that returns None-safe results.","Unit-test search builders with a zero-flags case."],"tags":["argument-validation","search-query","mailkit"],"backgroundTag":"empty-required-field","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}