Skip to content

Commit 1054804

Browse files
author
Pascal Hertleif
authored
Merge pull request #68 from projectgus/tweaks/codegen_warnings
Remove warnings in generated code
2 parents cbf65bb + 71ba20c commit 1054804

File tree

4 files changed

+87
-15
lines changed

4 files changed

+87
-15
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ fn main() {
3636
let config = Config::builder()
3737
.dbc_name("example.dbc")
3838
.dbc_content(&dbc_file)
39+
//.allow_dead_code(true) // Don't emit warnings if not all generated code is used
3940
//.impl_arbitrary(FeatureConfig::Gated("arbitrary")) // Optional impls.
4041
//.impl_debug(FeatureConfig::Always) // See rustdoc for more,
4142
//.check_ranges(FeatureConfig::Never) // or look below for an example.

src/lib.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,13 @@ pub struct Config<'a> {
7373
#[builder(default)]
7474
pub impl_error: FeatureConfig<'a>,
7575

76-
/// Optional: Validate min and max values in generated signal setters. Default: `Always`.
76+
/// Optional: Validate min and max values in generated signal setters. Default: `Always`
7777
#[builder(default = FeatureConfig::Always)]
7878
pub check_ranges: FeatureConfig<'a>,
79+
80+
/// Optional: Allow dead code in the generated module. Default: `false`.
81+
#[builder(default)]
82+
pub allow_dead_code: bool,
7983
}
8084

8185
/// Configuration for including features in the codegenerator.
@@ -112,16 +116,19 @@ pub fn codegen(config: Config<'_>, out: impl Write) -> Result<()> {
112116
writeln!(&mut w, "// Generated code!")?;
113117
writeln!(
114118
&mut w,
115-
"#![allow(unused_comparisons, unreachable_patterns)]"
119+
"#![allow(unused_comparisons, unreachable_patterns, unused_imports)]"
116120
)?;
121+
if config.allow_dead_code {
122+
writeln!(&mut w, "#![allow(dead_code)]")?;
123+
}
117124
writeln!(&mut w, "#![allow(clippy::let_and_return, clippy::eq_op)]")?;
118125
writeln!(
119126
&mut w,
120127
"#![allow(clippy::useless_conversion, clippy::unnecessary_cast)]"
121128
)?;
122129
writeln!(
123130
&mut w,
124-
"#![allow(clippy::excessive_precision, clippy::manual_range_contains, clippy::absurd_extreme_comparisons)]"
131+
"#![allow(clippy::excessive_precision, clippy::manual_range_contains, clippy::absurd_extreme_comparisons, clippy::too_many_arguments)]"
125132
)?;
126133
writeln!(&mut w, "#![deny(clippy::arithmetic_side_effects)]")?;
127134
writeln!(&mut w)?;
@@ -326,7 +333,8 @@ fn render_message(mut w: impl Write, config: &Config<'_>, msg: &Message, dbc: &D
326333
let mut w = PadAdapter::wrap(&mut w);
327334
writeln!(
328335
&mut w,
329-
"let mut res = Self {{ raw: [0u8; {}] }};",
336+
"let {}res = Self {{ raw: [0u8; {}] }};",
337+
if msg.signals().is_empty() { "" } else { "mut " },
330338
msg.message_size()
331339
)?;
332340
for signal in msg.signals().iter() {
@@ -1347,21 +1355,22 @@ fn render_arbitrary(mut w: impl Write, config: &Config<'_>, msg: &Message) -> Re
13471355
typ = type_name(msg.message_name())
13481356
)?;
13491357
{
1358+
let filtered_signals: Vec<&Signal> = msg
1359+
.signals()
1360+
.iter()
1361+
.filter(|signal| {
1362+
*signal.multiplexer_indicator() == MultiplexIndicator::Plain
1363+
|| *signal.multiplexer_indicator() == MultiplexIndicator::Multiplexor
1364+
})
1365+
.collect();
13501366
let mut w = PadAdapter::wrap(&mut w);
13511367
writeln!(
13521368
w,
1353-
"fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self, arbitrary::Error> {{"
1369+
"fn arbitrary({}u: &mut Unstructured<'a>) -> Result<Self, arbitrary::Error> {{",
1370+
if filtered_signals.is_empty() { "_" } else { "" },
13541371
)?;
13551372
{
13561373
let mut w = PadAdapter::wrap(&mut w);
1357-
let filtered_signals: Vec<&Signal> = msg
1358-
.signals()
1359-
.iter()
1360-
.filter(|signal| {
1361-
*signal.multiplexer_indicator() == MultiplexIndicator::Plain
1362-
|| *signal.multiplexer_indicator() == MultiplexIndicator::Multiplexor
1363-
})
1364-
.collect();
13651374

13661375
for signal in filtered_signals.iter() {
13671376
writeln!(

testing/can-messages/src/messages.rs

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// Generated code!
2-
#![allow(unused_comparisons, unreachable_patterns)]
2+
#![allow(unused_comparisons, unreachable_patterns, unused_imports)]
33
#![allow(clippy::let_and_return, clippy::eq_op)]
44
#![allow(clippy::useless_conversion, clippy::unnecessary_cast)]
55
#![allow(
66
clippy::excessive_precision,
77
clippy::manual_range_contains,
8-
clippy::absurd_extreme_comparisons
8+
clippy::absurd_extreme_comparisons,
9+
clippy::too_many_arguments
910
)]
1011
#![deny(clippy::arithmetic_side_effects)]
1112

@@ -35,6 +36,8 @@ pub enum Messages {
3536
MultiplexTest(MultiplexTest),
3637
/// IntegerFactorOffset
3738
IntegerFactorOffset(IntegerFactorOffset),
39+
/// MsgWithoutSignals
40+
MsgWithoutSignals(MsgWithoutSignals),
3841
}
3942

4043
impl Messages {
@@ -49,6 +52,7 @@ impl Messages {
4952
1028 => Messages::Dolor(Dolor::try_from(payload)?),
5053
200 => Messages::MultiplexTest(MultiplexTest::try_from(payload)?),
5154
1337 => Messages::IntegerFactorOffset(IntegerFactorOffset::try_from(payload)?),
55+
513 => Messages::MsgWithoutSignals(MsgWithoutSignals::try_from(payload)?),
5256
n => return Err(CanError::UnknownMessageId(n)),
5357
};
5458
Ok(res)
@@ -1822,6 +1826,62 @@ impl<'a> Arbitrary<'a> for IntegerFactorOffset {
18221826
}
18231827
}
18241828

1829+
/// MsgWithoutSignals
1830+
///
1831+
/// - ID: 513 (0x201)
1832+
/// - Size: 8 bytes
1833+
/// - Transmitter: Ipsum
1834+
#[derive(Clone, Copy)]
1835+
pub struct MsgWithoutSignals {
1836+
raw: [u8; 8],
1837+
}
1838+
1839+
impl MsgWithoutSignals {
1840+
pub const MESSAGE_ID: u32 = 513;
1841+
1842+
/// Construct new MsgWithoutSignals from values
1843+
pub fn new() -> Result<Self, CanError> {
1844+
let res = Self { raw: [0u8; 8] };
1845+
Ok(res)
1846+
}
1847+
1848+
/// Access message payload raw value
1849+
pub fn raw(&self) -> &[u8; 8] {
1850+
&self.raw
1851+
}
1852+
}
1853+
1854+
impl core::convert::TryFrom<&[u8]> for MsgWithoutSignals {
1855+
type Error = CanError;
1856+
1857+
#[inline(always)]
1858+
fn try_from(payload: &[u8]) -> Result<Self, Self::Error> {
1859+
if payload.len() != 8 {
1860+
return Err(CanError::InvalidPayloadSize);
1861+
}
1862+
let mut raw = [0u8; 8];
1863+
raw.copy_from_slice(&payload[..8]);
1864+
Ok(Self { raw })
1865+
}
1866+
}
1867+
1868+
impl core::fmt::Debug for MsgWithoutSignals {
1869+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1870+
if f.alternate() {
1871+
f.debug_struct("MsgWithoutSignals").finish()
1872+
} else {
1873+
f.debug_tuple("MsgWithoutSignals").field(&self.raw).finish()
1874+
}
1875+
}
1876+
}
1877+
1878+
#[cfg(feature = "arb")]
1879+
impl<'a> Arbitrary<'a> for MsgWithoutSignals {
1880+
fn arbitrary(_u: &mut Unstructured<'a>) -> Result<Self, arbitrary::Error> {
1881+
MsgWithoutSignals::new().map_err(|_| arbitrary::Error::IncorrectFormat)
1882+
}
1883+
}
1884+
18251885
/// This is just to make testing easier
18261886
#[allow(dead_code)]
18271887
fn main() {}

testing/dbc-examples/example.dbc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ BO_ 1337 IntegerFactorOffset: 8 Sit
4747
SG_ ByteWithNegativeOffset : 24|8@1+ (1,-1) [0|255] "" Vector__XXX
4848
SG_ ByteWithNegativeMin : 32|8@1+ (1,-1) [-127|127] "" Vector__XXX
4949

50+
BO_ 513 MsgWithoutSignals: 8 Ipsum
51+
5052
VAL_ 512 Three 0 "OFF" 1 "ON" 2 "ONER" 3 "ONEST";
5153
VAL_ 512 Four 0 "Off" 1 "On" 2 "Oner" 3 "Onest";
5254
VAL_ 512 Type 0 "0Off" 1 "1On";

0 commit comments

Comments
 (0)