Skip to content

Commit 930bd2a

Browse files
authored
Add must_use attributes and enable clippy::must-use-candidate (#232)
If a semicolon discards the result of a function or method tagged with `#[must_use]`, the compiler will emit a lint message warning that the return value is expected to be used. This lint suggests adding `#[must_use]` to public functions that: - return something that's not already marked as `must_use` - have no mutable *reference* args (having a mutable owned arg is fine, since as the method took ownership, the caller can only access the result via the returned self, so it therefore still must be used) - don't mutate statics Docs: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute https://rust-lang.github.io/rust-clippy/master/index.html#must_use_candidate For more on best practices of when to use `must_use`, see: rust-lang/rust#48926 (comment) Fixes #57. GUS-W-10222390.
1 parent ac8f17f commit 930bd2a

11 files changed

+29
-6
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## [Unreleased]
44

5+
- Add `must_use` attributes to a number of pure public methods.
6+
57
## [0.4.0] 2021/12/08
68

79
- Add `PartialEq` and `Eq` implementations for `Process`.

libcnb-data/src/build_plan.rs

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub struct BuildPlan {
1313
}
1414

1515
impl BuildPlan {
16+
#[must_use]
1617
pub fn new() -> Self {
1718
Self {
1819
provides: vec![],
@@ -35,6 +36,7 @@ pub struct BuildPlanBuilder {
3536
}
3637

3738
impl BuildPlanBuilder {
39+
#[must_use]
3840
pub fn new() -> Self {
3941
Self {
4042
acc: VecDeque::new(),
@@ -53,6 +55,7 @@ impl BuildPlanBuilder {
5355
self
5456
}
5557

58+
#[must_use]
5659
pub fn or(mut self) -> Self {
5760
self.acc
5861
.push_back((self.current_provides, self.current_requires));
@@ -62,6 +65,7 @@ impl BuildPlanBuilder {
6265
self
6366
}
6467

68+
#[must_use]
6569
pub fn build(self) -> BuildPlan {
6670
let mut xyz = self.or();
6771

libcnb-data/src/launch.rs

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub struct Launch {
2929
/// assert!(toml::to_string(&launch_toml).is_ok());
3030
/// ```
3131
impl Launch {
32+
#[must_use]
3233
pub fn new() -> Self {
3334
Self {
3435
bom: bom::Bom::new(),
@@ -38,6 +39,7 @@ impl Launch {
3839
}
3940
}
4041

42+
#[must_use]
4143
pub fn process(mut self, process: Process) -> Self {
4244
self.processes.push(process);
4345
self

libcnb-data/src/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
#![warn(clippy::pedantic)]
88
// This lint is too noisy and enforces a style that reduces readability in many cases.
99
#![allow(clippy::module_name_repetitions)]
10-
// Re-disable pedantic lints that are currently failing, until they are triaged and fixed/wontfixed.
11-
// https://github.com/Malax/libcnb.rs/issues/57
12-
#![allow(clippy::must_use_candidate)]
1310

1411
pub mod bom;
1512
pub mod build;

libcnb/src/build.rs

+4
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ pub struct BuildResultBuilder {
146146
}
147147

148148
impl BuildResultBuilder {
149+
#[must_use]
149150
pub fn new() -> Self {
150151
Self {
151152
launch: None,
@@ -166,18 +167,21 @@ impl BuildResultBuilder {
166167
Ok(self.build_unwrapped())
167168
}
168169

170+
#[must_use]
169171
pub fn build_unwrapped(self) -> BuildResult {
170172
BuildResult(InnerBuildResult::Pass {
171173
launch: self.launch,
172174
store: self.store,
173175
})
174176
}
175177

178+
#[must_use]
176179
pub fn launch(mut self, launch: Launch) -> Self {
177180
self.launch = Some(launch);
178181
self
179182
}
180183

184+
#[must_use]
181185
pub fn store(mut self, store: Store) -> Self {
182186
self.store = Some(store);
183187
self

libcnb/src/detect.rs

+5
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ pub(crate) enum InnerDetectResult {
4646
pub struct DetectResultBuilder;
4747

4848
impl DetectResultBuilder {
49+
#[must_use]
4950
pub fn pass() -> PassDetectResultBuilder {
5051
PassDetectResultBuilder { build_plan: None }
5152
}
5253

54+
#[must_use]
5355
pub fn fail() -> FailDetectResultBuilder {
5456
FailDetectResultBuilder {}
5557
}
@@ -73,12 +75,14 @@ impl PassDetectResultBuilder {
7375
Ok(self.build_unwrapped())
7476
}
7577

78+
#[must_use]
7679
pub fn build_unwrapped(self) -> DetectResult {
7780
DetectResult(InnerDetectResult::Pass {
7881
build_plan: self.build_plan,
7982
})
8083
}
8184

85+
#[must_use]
8286
pub fn build_plan(mut self, build_plan: BuildPlan) -> Self {
8387
self.build_plan = Some(build_plan);
8488
self
@@ -102,6 +106,7 @@ impl FailDetectResultBuilder {
102106
}
103107

104108
#[allow(clippy::unused_self)]
109+
#[must_use]
105110
pub fn build_unwrapped(self) -> DetectResult {
106111
DetectResult(InnerDetectResult::Fail)
107112
}

libcnb/src/env.rs

+5
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ impl Env {
3838
/// variables afterwards will not be reflected in the returned value.
3939
///
4040
/// See [`std::env::vars_os`]
41+
#[must_use]
4142
pub fn from_current() -> Self {
4243
env::vars_os().into()
4344
}
4445

4546
/// Creates an empty `Env` struct.
47+
#[must_use]
4648
pub fn new() -> Self {
4749
Self {
4850
inner: HashMap::new(),
@@ -57,15 +59,18 @@ impl Env {
5759
}
5860

5961
/// Returns a cloned value corresponding to the given key.
62+
#[must_use]
6063
pub fn get(&self, key: impl AsRef<OsStr>) -> Option<OsString> {
6164
self.inner.get(key.as_ref()).cloned()
6265
}
6366

6467
/// Returns true if the environment contains a value for the specified key.
68+
#[must_use]
6569
pub fn contains_key(&self, key: impl AsRef<OsStr>) -> bool {
6670
self.inner.contains_key(key.as_ref())
6771
}
6872

73+
#[must_use]
6974
pub fn iter(&self) -> std::collections::hash_map::Iter<'_, OsString, OsString> {
7075
self.inner.iter()
7176
}

libcnb/src/generic.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub struct GenericPlatform {
2424
}
2525

2626
impl GenericPlatform {
27+
#[must_use]
2728
pub fn new(env: Env) -> Self {
2829
Self { env }
2930
}

libcnb/src/layer/public_interface.rs

+3
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,15 @@ pub struct LayerResultBuilder<M> {
175175
}
176176

177177
impl<M> LayerResultBuilder<M> {
178+
#[must_use]
178179
pub fn new(metadata: M) -> Self {
179180
Self {
180181
metadata,
181182
env: None,
182183
}
183184
}
184185

186+
#[must_use]
185187
pub fn env(mut self, layer_env: LayerEnv) -> Self {
186188
self.env = Some(layer_env);
187189
self
@@ -199,6 +201,7 @@ impl<M> LayerResultBuilder<M> {
199201
Ok(self.build_unwrapped())
200202
}
201203

204+
#[must_use]
202205
pub fn build_unwrapped(self) -> LayerResult<M> {
203206
LayerResult {
204207
metadata: self.metadata,

libcnb/src/layer_env.rs

+3
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ impl LayerEnv {
113113
/// let modified_env = layer_env.apply(TargetLifecycle::Build, &env);
114114
/// assert_eq!(env, modified_env);
115115
/// ```
116+
#[must_use]
116117
pub fn new() -> Self {
117118
Self {
118119
all: LayerEnvDelta::new(),
@@ -143,6 +144,7 @@ impl LayerEnv {
143144
/// assert_eq!(modified_env.get("VAR").unwrap(), "foobar");
144145
/// assert_eq!(modified_env.get("VAR2").unwrap(), "previous-value");
145146
/// ```
147+
#[must_use]
146148
pub fn apply(&self, target: TargetLifecycle, env: &Env) -> Env {
147149
let deltas = match target {
148150
TargetLifecycle::All => vec![&self.all],
@@ -243,6 +245,7 @@ impl LayerEnv {
243245
/// ),
244246
/// );
245247
/// ```
248+
#[must_use]
246249
pub fn chainable_insert(
247250
mut self,
248251
target: TargetLifecycle,

libcnb/src/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
#![allow(clippy::module_name_repetitions)]
1212
// This lint triggers when both layer_dir and layers_dir are present which are quite common.
1313
#![allow(clippy::similar_names)]
14-
// Re-disable pedantic lints that are currently failing, until they are triaged and fixed/wontfixed.
15-
// https://github.com/Malax/libcnb.rs/issues/57
16-
#![allow(clippy::must_use_candidate)]
1714

1815
pub mod build;
1916
pub mod detect;

0 commit comments

Comments
 (0)