dlang-community / D-Scanner

Swiss-army knife for D source code
Boost Software License 1.0
240 stars 80 forks source link

Add check for `is(T : ...)` tests testing for noreturn #863

Open WebFreak001 opened 2 years ago

WebFreak001 commented 2 years ago

See https://forum.dlang.org/thread/xmjabqpofltdwxfqlwhi@forum.dlang.org

code like

struct Something(Types...) {}

enum isSomething(T) = is(T : Something!Types, Types...);

might be how users write custom is-checks to see if a given type is some templated type, however for isSomething!noreturn this returns true, which might be unexpected for the user. (see forum discussion)

My check idea to add would be warning if noreturn isn't explicitly handled for global templates or maybe even everywhere. (could be configurable)

so for the code above it would give a warning and the user would have a few choices to fix it:

// noreturn is not Something
enum isSomething(T) = is(T : Something!Types, Types...)
    && !is(immutable T == immutable noreturn);

// noreturn is Something
enum isSomething(T) = is(T : Something!Types, Types...)
    || is(immutable T == immutable noreturn);

// only match exact Something (no alias this support anymore though)
enum isSomething(T) = is(T == Something!Types, Types...);

to keep the check simple it would just check for any is(T : ...) expression and see if in the same expression or template T is compared with noreturn.