andreasbuhr / cppcoro

A library of C++ coroutine abstractions for the coroutines TS
MIT License
364 stars 53 forks source link

"Warning: switch missing default case" when using "generator<int>" #32

Open FalcoGer opened 3 years ago

FalcoGer commented 3 years ago

I get an odd warning for the following code I made for testing the library:

#include <iostream>
#include <cmath>
#include <cppcoro/generator.hpp>

using std::cout;
using std::endl;

cppcoro::generator<int> primes();

int main()
{
    int i = 0;
    for (auto p : primes())
    {
        cout << ++i << ": " << p << endl;
        if (i >= 10000)
        {
            break;
        }
    }
    return 0;
}

cppcoro::generator<int> primes()
{
    // simplified prime check
    // no p <= 1 check
    // no checking of div 2 or any other even number
    // can be simplified because we only test odd numbers >= 5 anyway
    const auto pc = [](const int& val) {
        for (int i = 3; i <= sqrt(val); i+=2)
            if (val % i == 0)
                return false;
        return true;
    };
    co_yield 2;
    int i = 3;
    while (true) {
        co_yield i;
        do {
            i+=2;
        }while (!pc(i));
    }
} // line 41 (warning is here)

My compiler output is the following:

/bin/sh -c '/usr/bin/make -j4 -e -f  Makefile'
----------Building project:[ coroutines - Release ]----------
make[1]: Entering directory '/home/****/Documents/Projects/coroutines'
/usr/bin/g++-10  -c  "/home/****/Documents/Projects/coroutines/main.cpp" -O2 -Wdouble-promotion -Wformat=2 -Wformat-nonliteral -Wformat-signedness -Wformat-y2k -Wnull-dereference -Wimplicit-fallthrough=2 -Wmissing-include-dirs -Wswitch-default -Wunused-parameter -Wuninitialized -Wsuggest-attribute=const -Walloc-zero -Walloca -Wconversion -Wfloat-conversion -Wsign-conversion -Wduplicated-branches -Wduplicated-cond -Wtrampolines -Wfloat-equal -Wshadow=compatible-local -Wundef -Wunused-macros -Wcast-qual -Wcast-align=strict -Wlogical-op -Wmissing-declarations -Wredundant-decls -Wstack-protector -fstack-protector -pedantic-errors -Werror=pedantic -Werror=char-subscripts -Werror=null-dereference -Werror=init-self -Werror=implicit-fallthrough=2 -Werror=misleading-indentation -Werror=missing-braces -Werror=multistatement-macros -Werror=sequence-point -Werror=return-type -Werror=multichar -pedantic -W -std=c++20 -fcoroutines -Wall -DNDEBUG  -o ./Release/main.cpp.o -I. -I.
/home/****/Documents/Projects/coroutines/main.cpp: In function 'cppcoro::generator<int> primes()':
/home/****/Documents/Projects/coroutines/main.cpp:44:1: warning: switch missing default case [-Wswitch-default]
   44 | }
      | ^
/home/****/Documents/Projects/coroutines/main.cpp:44:1: warning: switch missing default case [-Wswitch-default]
/home/****/Documents/Projects/coroutines/main.cpp:44:1: warning: switch missing default case [-Wswitch-default]
/home/****/Documents/Projects/coroutines/main.cpp:44:1: warning: switch missing default case [-Wswitch-default]
/home/****/Documents/Projects/coroutines/main.cpp:44:1: warning: switch missing default case [-Wswitch-default]
/home/****/Documents/Projects/coroutines/main.cpp:44:1: warning: switch missing default case [-Wswitch-default]
/usr/bin/g++-10 -o ./Release/coroutines @"coroutines.txt" -L.
make[1]: Leaving directory '/home/****/Documents/Projects/coroutines'
====0 errors, 6 warnings====

I am not sure why that is. I do not use any switch statements at all. The warning is always on the closing bracket of the primes() coroutine, even if I move it above main. It's not EOF related.

ps: Thank you for fixing the original cppcoro and using a sane build system!