git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 3/3] grep: get rid of useless x < 0 comparison on an enum member

From
Jeff King <peff@peff.net>
Date
Nov 7, 2011, 16:38 UTC
Message-ID
<20111107163823.GB27055@sigill.intra.peff.net>
In-Reply-To
<m3pqh4krer.fsf@hase.home>
On Mon, Nov 07, 2011 at 02:12:28PM +0100, Andreas Schwab wrote:
Show 9 quoted lines
> Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:
> 
> > I.e. we'll always have GREP_HEADER_AUTHOR = 0 and
> > GREP_HEADER_COMMITTER = 1, we'll never have GREP_HEADER_AUTHOR = and
> > GREP_HEADER_COMMITTER = <some int>.
> 
> That is irrelevant.  You can always assign -1 to an object of enumerated
> type and the implicit conversion to the underlying type is fully
> defined, no matter what type the compiler choses.

Yes, but now you are getting into implementation-defined behavior, which is also something to avoid. IOW, I don't think it's wrong for a static analyzer to complain about:

  if (enum_with_only_positive_values < 0)
just because you could say:
  enum_with_only_positive_values = -1;

and it would work on _some_ platforms. That same static analyzer should be complaining about the second line, which is where the real potential bug is. If you want "-1" as an enumerated value, then add it to your enum definition and the compiler will do the right thing.

It is perfectly fine to do:
     enum foo { a = -1, b, c };
     ...
     if (foo < 0)
        ...
and the static analyzer should not complain about the conditional there.
-Peff
Previous: Andreas SchwabNext: Andreas Schwab
Message 10 of 22 in “Fix code issues spotted by clang”
  1. 0/3 Fix code issues spotted by clangÆvar Arnfjörð Bjarmason, Nov 6, 2011
  2. 1/3 apply: get rid of useless x < 0 comparison on a size_t typeÆvar Arnfjörð Bjarmason, Nov 6, 2011
  3. Junio C HamanoNov 6, 2011
  4. Giuseppe BilottaNov 7, 2011
  5. 2/3 diff/apply: cast variable in call to free()Ævar Arnfjörð Bjarmason, Nov 6, 2011
  6. 3/3 grep: get rid of useless x < 0 comparison on an enum memberÆvar Arnfjörð Bjarmason, Nov 6, 2011
  7. Andreas SchwabNov 6, 2011
  8. Ævar Arnfjörð BjarmasonNov 7, 2011
  9. Andreas SchwabNov 7, 2011
  10. Jeff KingNov 7, 2011
  11. Andreas SchwabNov 7, 2011
  12. Jeff KingNov 7, 2011
  13. Jeff KingNov 7, 2011
  14. Ævar Arnfjörð BjarmasonNov 7, 2011
  15. Andreas SchwabNov 7, 2011
  16. Jonathan NiederNov 7, 2011
  17. Junio C HamanoNov 7, 2011
  18. Jonathan NiederNov 7, 2011
  19. Junio C HamanoNov 7, 2011
  20. Jonathan NiederNov 7, 2011
  21. Ævar Arnfjörð BjarmasonNov 6, 2011
  22. Elijah NewrenNov 8, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.