tests/lint: add test for narrowing conversions

Lint can warn about narrowing conversions, it just doesn't do so by
default.

The option -a (which is included in the default LINTFLAGS in sys.mk)
only reports narrowing conversions from 'long' or larger.  To get
warnings about all possible narrowing conversions, the option -a has to
be given more than once.

PR bin/14531
This commit is contained in:
rillig 2021-02-28 21:39:17 +00:00
parent 4bcd6a6eb8
commit 84a7228e25
2 changed files with 72 additions and 4 deletions

View File

@ -1,7 +1,64 @@
/* $NetBSD: msg_132.c,v 1.2 2021/02/21 09:07:58 rillig Exp $ */
/* $NetBSD: msg_132.c,v 1.3 2021/02/28 21:39:17 rillig Exp $ */
# 3 "msg_132.c"
// Test for message: conversion from '%s' to '%s' may lose accuracy [132]
TODO: "Add example code that triggers the above message." /* expect: 249 */
TODO: "Add example code that almost triggers the above message."
/*
* NetBSD's default lint flags only include a single -a, which only flags
* narrowing conversions from long. To get warnings for all narrowing
* conversions, -aa needs to be given more than once.
*
* https://gnats.netbsd.org/14531
*/
/* lint1-extra-flags: -aa */
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long long u64;
typedef signed char i8;
typedef signed short i16;
typedef signed int i32;
typedef signed long long i64;
void
convert_unsigned(u8 v8, u16 v16, u32 v32, u64 v64)
{
v8 = v16; /* expect: 132 */
v8 = v32; /* expect: 132 */
v8 = v64; /* expect: 132 */
v16 = v8;
v16 = v32; /* expect: 132 */
v16 = v64; /* expect: 132 */
v32 = v8;
v32 = v16;
v32 = v64; /* expect: 132 */
v64 = v8;
v64 = v16;
v64 = v32;
}
void
convert_signed(i8 v8, i16 v16, i32 v32, i64 v64)
{
v8 = v16; /* expect: 132 */
v8 = v32; /* expect: 132 */
v8 = v64; /* expect: 132 */
v16 = v8;
v16 = v32; /* expect: 132 */
v16 = v64; /* expect: 132 */
v32 = v8;
v32 = v16;
v32 = v64; /* expect: 132 */
v64 = v8;
v64 = v16;
v64 = v32;
}

View File

@ -1 +1,12 @@
msg_132.c(6): syntax error ':' [249]
msg_132.c(29): warning: conversion from 'unsigned short' to 'unsigned char' may lose accuracy [132]
msg_132.c(30): warning: conversion from 'unsigned int' to 'unsigned char' may lose accuracy [132]
msg_132.c(31): warning: conversion from 'unsigned long long' to 'unsigned char' may lose accuracy [132]
msg_132.c(34): warning: conversion from 'unsigned int' to 'unsigned short' may lose accuracy [132]
msg_132.c(35): warning: conversion from 'unsigned long long' to 'unsigned short' may lose accuracy [132]
msg_132.c(39): warning: conversion from 'unsigned long long' to 'unsigned int' may lose accuracy [132]
msg_132.c(49): warning: conversion from 'short' to 'signed char' may lose accuracy [132]
msg_132.c(50): warning: conversion from 'int' to 'signed char' may lose accuracy [132]
msg_132.c(51): warning: conversion from 'long long' to 'signed char' may lose accuracy [132]
msg_132.c(54): warning: conversion from 'int' to 'short' may lose accuracy [132]
msg_132.c(55): warning: conversion from 'long long' to 'short' may lose accuracy [132]
msg_132.c(59): warning: conversion from 'long long' to 'int' may lose accuracy [132]