Menu

Proposal new check endl << endl

federico
2024-06-28
2024-07-11
  • federico

    federico - 2024-06-28

    In a project I noticed following pattern

    void f()
    {
    std::cout << "a" << std::endl << std::endl;
    }
    

    And a similar patterns one with flush, like "std::endl << std::flush"

    Fortunately, it was easy to write a custom check

    <?xml version="1.0"?>
    <rule version="1">
        <pattern>std :: (endl|flush) &lt;&lt; std :: (endl|flush)</pattern>
        <message>
            <id>redundantCondition</id>
            <severity>performance</severity>
        <summary>Multiple flushes. Consider flushing only once, and replacing "std::endl" with "\n"</summary>
        </message>
    </rule>
    

    I would prefer if cppcheck would do a similar verification by itself; what are the criteria for adding a new check in cppcheck?

    Note that this pattern is not an error per se, but it hurts performance, as in general "\n" will already do a flush, and std::endl always flushes.

     
  • CHR

    CHR - 2024-07-01

    clang -tidy detects any endl usage as performance-avoid-endl.

     
  • Daniel Marjamäki

    I am not against it. your suggestion sounds less aggressive than clang-tidy 👍

    The main criteria is that it will be low noise. And well if you warn about redundant flushes that does not sound very noisy to me.

     
  • federico

    federico - 2024-07-11

    Exactly, warning about all std::endl might be too much (used in a lot of places, and it might be intended), while two consecutive flushes is most likely unintended.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.