lint: since C99, a non-void function must return a value

This commit is contained in:
rillig 2023-08-06 19:44:50 +00:00
parent dca1e8c791
commit fe7ce87087
12 changed files with 33 additions and 32 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: c11_generic_expression.c,v 1.18 2023/07/09 11:18:55 rillig Exp $ */
/* $NetBSD: c11_generic_expression.c,v 1.19 2023/08/06 19:44:50 rillig Exp $ */
# 3 "c11_generic_expression.c"
/* lint1-extra-flags: -X 351 */
@ -17,7 +17,8 @@
/*
* The type of 'var' is not compatible with any of the types from the
* generic-association. This is a compile-time error.
* generic-association. This is a constraint violation that the compiler must
* detect, therefore lint doesn't repeat that diagnostic.
*/
const char *
classify_type_without_default(double var)
@ -29,7 +30,7 @@ classify_type_without_default(double var)
long long: "long long",
unsigned: "unsigned"
);
/* expect-1: warning: function 'classify_type_without_default' expects to return value [214] */
/* expect-1: error: function 'classify_type_without_default' expects to return value [214] */
}
/*

View File

@ -1,4 +1,4 @@
/* $NetBSD: d_c99_bool_strict_syshdr.c,v 1.21 2023/08/02 18:51:25 rillig Exp $ */
/* $NetBSD: d_c99_bool_strict_syshdr.c,v 1.22 2023/08/06 19:44:50 rillig Exp $ */
# 3 "d_c99_bool_strict_syshdr.c"
/*
@ -168,7 +168,7 @@ _Bool
str_equal_bad(const char *s1, const char *s2)
{
/* expect+2: error: operand of '!' must be bool, not 'int' [330] */
/* expect+1: warning: function 'str_equal_bad' expects to return value [214] */
/* expect+1: error: function 'str_equal_bad' expects to return value [214] */
return !strcmp(s1, s2);
}

View File

@ -81,6 +81,6 @@ void do_while_struct(struct s s) { do { return; } while (s); }
/* C99 6.5.15 does not require a scalar type, curiously. */
/* expect+3: error: first operand of '?' must have scalar type [170] */
/* expect+2: warning: function 'conditional_struct' expects to return value [214] */
/* expect+2: error: function 'conditional_struct' expects to return value [214] */
/* expect+1: warning: parameter 's' unused in function 'conditional_struct' [231] */
int conditional_struct(struct s s) { return s ? 1 : 2; }

View File

@ -1,4 +1,4 @@
/* $NetBSD: expr_cast.c,v 1.4 2023/07/07 19:45:22 rillig Exp $ */
/* $NetBSD: expr_cast.c,v 1.5 2023/08/06 19:44:50 rillig Exp $ */
# 3 "expr_cast.c"
/*
@ -28,6 +28,6 @@ cast(void)
};
/* expect+2: error: invalid cast from 'struct S' to 'struct S' [147] */
/* expect+1: warning: function 'cast' expects to return value [214] */
/* expect+1: error: function 'cast' expects to return value [214] */
return (struct S)local;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: lang_level_c99.c,v 1.1 2023/08/06 19:31:06 rillig Exp $ */
/* $NetBSD: lang_level_c99.c,v 1.2 2023/08/06 19:44:50 rillig Exp $ */
# 3 "lang_level_c99.c"
/*
@ -297,7 +297,7 @@ const const int duplicate_type_qualifier = 2;
//
// Irrelevant, as lint only sees the preprocessed source code.
// [-] return without expression not permitted in function that returns a value
// [x] return without expression not permitted in function that returns a value
// (and vice versa)
void
@ -311,7 +311,6 @@ return_no_expr(int x)
int
return_expr(void)
{
// FIXME: Make this an error in C99 and later.
/* expect+1: warning: function 'return_expr' expects to return value [214] */
/* expect+1: error: function 'return_expr' expects to return value [214] */
return;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: msg_109.c,v 1.6 2023/07/07 19:45:22 rillig Exp $ */
/* $NetBSD: msg_109.c,v 1.7 2023/08/06 19:44:50 rillig Exp $ */
# 3 "msg_109.c"
// Test for message: void type illegal in expression [109]
@ -10,6 +10,6 @@ int
example(int arg)
{
/* expect+2: error: void type illegal in expression [109] */
/* expect+1: warning: function 'example' expects to return value [214] */
/* expect+1: error: function 'example' expects to return value [214] */
return arg + (void)4;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: msg_126.c,v 1.7 2023/07/07 19:45:22 rillig Exp $ */
/* $NetBSD: msg_126.c,v 1.8 2023/08/06 19:44:50 rillig Exp $ */
# 3 "msg_126.c"
// Test for message: incompatible types '%s' and '%s' in conditional [126]
@ -10,6 +10,6 @@ int
max(int cond, void *ptr, double dbl)
{
/* expect+2: error: incompatible types 'pointer to void' and 'double' in conditional [126] */
/* expect+1: warning: function 'max' expects to return value [214] */
/* expect+1: error: function 'max' expects to return value [214] */
return cond ? ptr : dbl;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: msg_132.c,v 1.31 2023/06/03 20:28:54 rillig Exp $ */
/* $NetBSD: msg_132.c,v 1.32 2023/08/06 19:44:50 rillig Exp $ */
# 3 "msg_132.c"
// Test for message: conversion from '%s' to '%s' may lose accuracy [132]
@ -175,7 +175,7 @@ const char *
cover_build_plus_minus(const char *arr, double idx)
{
/* expect+3: error: operands of '+' have incompatible types 'pointer to const char' and 'double' [107] */
/* expect+2: warning: function 'cover_build_plus_minus' expects to return value [214] */
/* expect+2: error: function 'cover_build_plus_minus' expects to return value [214] */
if (idx > 0.0)
return arr + idx;
return arr + (unsigned int)idx;

View File

@ -1,4 +1,4 @@
/* $NetBSD: msg_170.c,v 1.7 2023/07/09 11:18:55 rillig Exp $ */
/* $NetBSD: msg_170.c,v 1.8 2023/08/06 19:44:50 rillig Exp $ */
# 3 "msg_170.c"
// Test for message: first operand of '?' must have scalar type [170]
@ -14,6 +14,6 @@ _Bool
example(const struct number *num)
{
/* expect+2: error: first operand of '?' must have scalar type [170] */
/* expect+1: warning: function 'example' expects to return value [214] */
/* expect+1: error: function 'example' expects to return value [214] */
return *num ? 1 : 0;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: msg_204.c,v 1.10 2023/08/02 18:51:25 rillig Exp $ */
/* $NetBSD: msg_204.c,v 1.11 2023/08/06 19:44:50 rillig Exp $ */
# 3 "msg_204.c"
// Test for message: controlling expressions must have scalar type [204]
@ -108,5 +108,5 @@ void do_while_struct(struct s s) { do { return; } while (s); }
* controlling expression must have a scalar type, curiously.
*/
/* expect+2: error: first operand of '?' must have scalar type [170] */
/* expect+1: warning: function 'conditional_struct' expects to return value [214] */
/* expect+1: error: function 'conditional_struct' expects to return value [214] */
int conditional_struct(struct s s) { return s ? 1 : 2; }

View File

@ -1,4 +1,4 @@
/* $NetBSD: msg_214.c,v 1.5 2023/07/07 19:45:22 rillig Exp $ */
/* $NetBSD: msg_214.c,v 1.6 2023/08/06 19:44:50 rillig Exp $ */
# 3 "msg_214.c"
// Test for message: function '%s' expects to return value [214]
@ -8,6 +8,6 @@
int
int_function(void)
{
/* expect+1: warning: function 'int_function' expects to return value [214] */
/* expect+1: error: function 'int_function' expects to return value [214] */
return;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: func.c,v 1.173 2023/08/02 18:51:25 rillig Exp $ */
/* $NetBSD: func.c,v 1.174 2023/08/06 19:44:50 rillig Exp $ */
/*
* Copyright (c) 1994, 1995 Jochen Pohl
@ -37,7 +37,7 @@
#include <sys/cdefs.h>
#if defined(__RCSID)
__RCSID("$NetBSD: func.c,v 1.173 2023/08/02 18:51:25 rillig Exp $");
__RCSID("$NetBSD: func.c,v 1.174 2023/08/06 19:44:50 rillig Exp $");
#endif
#include <stdlib.h>
@ -1052,12 +1052,13 @@ stmt_return(bool sys, tnode_t *tn)
error(213, funcsym->s_name);
expr_free_all();
tn = NULL;
} else if (tn == NULL && funcsym->s_type->t_subt->t_tspec != VOID) {
/*
* Assume that the function has a return value only if it
* is explicitly declared.
*/
if (!funcsym->s_return_type_implicit_int)
}
if (tn == NULL && funcsym->s_type->t_subt->t_tspec != VOID
&& !funcsym->s_return_type_implicit_int) {
if (allow_c99)
/* function '%s' expects to return value */
error(214, funcsym->s_name);
else
/* function '%s' expects to return value */
warning(214, funcsym->s_name);
}