Skip to content

Commit f038af9

Browse files
committed
Add some #[must_use] and other small things brought up from linting
1 parent 452363b commit f038af9

File tree

5 files changed

+29
-2
lines changed

5 files changed

+29
-2
lines changed

codespan-reporting/src/diagnostic.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ impl<FileId> Label<FileId> {
9090
}
9191

9292
/// Add a message to the diagnostic.
93+
#[must_use]
9394
pub fn with_message(mut self, message: impl Display) -> Label<FileId> {
9495
self.message = message.to_string();
9596
self
@@ -137,63 +138,73 @@ impl<FileId> Diagnostic<FileId> {
137138
/// Create a new diagnostic with a severity of [`Severity::Bug`].
138139
///
139140
/// [`Severity::Bug`]: Severity::Bug
141+
#[must_use]
140142
pub fn bug() -> Diagnostic<FileId> {
141143
Diagnostic::new(Severity::Bug)
142144
}
143145

144146
/// Create a new diagnostic with a severity of [`Severity::Error`].
145147
///
146148
/// [`Severity::Error`]: Severity::Error
149+
#[must_use]
147150
pub fn error() -> Diagnostic<FileId> {
148151
Diagnostic::new(Severity::Error)
149152
}
150153

151154
/// Create a new diagnostic with a severity of [`Severity::Warning`].
152155
///
153156
/// [`Severity::Warning`]: Severity::Warning
157+
#[must_use]
154158
pub fn warning() -> Diagnostic<FileId> {
155159
Diagnostic::new(Severity::Warning)
156160
}
157161

158162
/// Create a new diagnostic with a severity of [`Severity::Note`].
159163
///
160164
/// [`Severity::Note`]: Severity::Note
165+
#[must_use]
161166
pub fn note() -> Diagnostic<FileId> {
162167
Diagnostic::new(Severity::Note)
163168
}
164169

165170
/// Create a new diagnostic with a severity of [`Severity::Help`].
166171
///
167172
/// [`Severity::Help`]: Severity::Help
173+
#[must_use]
168174
pub fn help() -> Diagnostic<FileId> {
169175
Diagnostic::new(Severity::Help)
170176
}
171177

172178
/// Set the error code of the diagnostic.
179+
#[must_use]
173180
pub fn with_code(mut self, code: impl Display) -> Diagnostic<FileId> {
174181
self.code = Some(code.to_string());
175182
self
176183
}
177184

178185
/// Set the message of the diagnostic.
186+
#[must_use]
179187
pub fn with_message(mut self, message: impl Display) -> Diagnostic<FileId> {
180188
self.message = message.to_string();
181189
self
182190
}
183191

184192
/// Add a label to the diagnostic.
193+
#[must_use]
185194
pub fn with_label(mut self, label: Label<FileId>) -> Diagnostic<FileId> {
186195
self.labels.push(label);
187196
self
188197
}
189198

190199
/// Add some labels to the diagnostic.
200+
#[must_use]
191201
pub fn with_labels(mut self, mut labels: Vec<Label<FileId>>) -> Diagnostic<FileId> {
192202
self.labels.append(&mut labels);
193203
self
194204
}
195205

196206
/// Add some labels to the diagnostic.
207+
#[must_use]
197208
pub fn with_labels_iter(
198209
mut self,
199210
labels: impl IntoIterator<Item = Label<FileId>>,
@@ -204,18 +215,21 @@ impl<FileId> Diagnostic<FileId> {
204215

205216
/// Add a note to the diagnostic.
206217
#[allow(clippy::needless_pass_by_value)]
218+
#[must_use]
207219
pub fn with_note(mut self, note: impl ToString) -> Diagnostic<FileId> {
208220
self.notes.push(note.to_string());
209221
self
210222
}
211223

212224
/// Add some notes to the diagnostic.
225+
#[must_use]
213226
pub fn with_notes(mut self, mut notes: Vec<String>) -> Diagnostic<FileId> {
214227
self.notes.append(&mut notes);
215228
self
216229
}
217230

218231
/// Add some notes to the diagnostic.
232+
#[must_use]
219233
pub fn with_notes_iter(
220234
mut self,
221235
notes: impl IntoIterator<Item = String>,

codespan-reporting/src/files.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ pub struct Location {
224224
/// assert_eq!(files::column_index(source, 2..13, 2 + 11), 3);
225225
/// assert_eq!(files::column_index(source, 2..13, 2 + 12), 3);
226226
/// ```
227+
#[must_use]
227228
pub fn column_index(source: &str, line_range: Range<usize>, byte_index: usize) -> usize {
228229
let end_index = core::cmp::min(byte_index, core::cmp::min(line_range.end, source.len()));
229230

@@ -383,6 +384,7 @@ where
383384
Source: AsRef<str>,
384385
{
385386
/// Create a new files database.
387+
#[must_use]
386388
pub fn new() -> SimpleFiles<Name, Source> {
387389
SimpleFiles { files: Vec::new() }
388390
}

codespan-reporting/src/term/config.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ pub mod styles {
152152

153153
impl Styles {
154154
/// The style used to mark a header at a given severity.
155+
#[must_use]
155156
pub fn header(&self, severity: Severity) -> &ColorSpec {
156157
match severity {
157158
Severity::Bug => &self.header_bug,
@@ -162,23 +163,28 @@ pub mod styles {
162163
}
163164
}
164165

166+
#[must_use]
165167
pub fn header_message(&self) -> &ColorSpec {
166168
&self.header_message
167169
}
168170

171+
#[must_use]
169172
pub fn line_number(&self) -> &ColorSpec {
170173
&self.line_number
171174
}
172175

176+
#[must_use]
173177
pub fn note_bullet(&self) -> &ColorSpec {
174178
&self.note_bullet
175179
}
176180

181+
#[must_use]
177182
pub fn source_border(&self) -> &ColorSpec {
178183
&self.source_border
179184
}
180185

181186
/// The style used to mark a primary or secondary label at a given severity.
187+
#[must_use]
182188
pub fn label(&self, severity: Severity, label_style: LabelStyle) -> &ColorSpec {
183189
match (label_style, severity) {
184190
(LabelStyle::Primary, Severity::Bug) => &self.primary_label_bug,
@@ -192,6 +198,7 @@ pub mod styles {
192198
}
193199

194200
impl Styles {
201+
#[must_use]
195202
pub fn no_color() -> Styles {
196203
Styles {
197204
header_bug: ColorSpec::new(),
@@ -416,6 +423,7 @@ impl Default for Chars {
416423

417424
impl Chars {
418425
/// A character set that uses Unicode box drawing characters.
426+
#[must_use]
419427
pub fn box_drawing() -> Chars {
420428
Chars {
421429
snippet_start: "┌─".into(),
@@ -446,6 +454,7 @@ impl Chars {
446454
/// This is useful if your terminal's font does not support box drawing
447455
/// characters well and results in output that looks similar to rustc's
448456
/// diagnostic output.
457+
#[must_use]
449458
pub fn ascii() -> Chars {
450459
Chars {
451460
snippet_start: "-->".into(),

codespan-reporting/src/term/renderer.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ impl<'writer, 'config> Renderer<'writer, 'config> {
602602
let current_label_style = single_labels
603603
.iter()
604604
.filter(|(_, range, _)| is_overlapping(range, &column_range))
605-
.map(|(label_style, _, _)| label_style.clone())
605+
.map(|(label_style, _, _)| *label_style)
606606
.max_by_key(label_priority_key);
607607

608608
// Update writer style if necessary
@@ -925,7 +925,7 @@ impl<'writer, 'config> Renderer<'writer, 'config> {
925925
let column_range = metrics.byte_index..(metrics.byte_index + ch.len_utf8());
926926
let label_style = hanging_labels(single_labels, trailing_label)
927927
.filter(|(_, range, _)| column_range.contains(&range.start))
928-
.map(|(label_style, _, _)| label_style.clone())
928+
.map(|(label_style, _, _)| *label_style)
929929
.max_by_key(label_priority_key);
930930

931931
let mut spaces = match label_style {
@@ -1199,6 +1199,7 @@ fn is_overlapping(range0: &Range<usize>, range1: &Range<usize>) -> bool {
11991199
}
12001200

12011201
/// For prioritizing primary labels over secondary labels when rendering carets.
1202+
#[allow(clippy::trivially_copy_pass_by_ref)]
12021203
fn label_priority_key(label_style: &LabelStyle) -> u8 {
12031204
match label_style {
12041205
LabelStyle::Secondary => 0,

codespan-reporting/src/term/views.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ impl<'diagnostic, 'config, FileId> RichDiagnostic<'diagnostic, 'config, FileId>
2323
where
2424
FileId: Copy + PartialEq,
2525
{
26+
#[must_use]
2627
pub fn new(
2728
diagnostic: &'diagnostic Diagnostic<FileId>,
2829
config: &'config Config,

0 commit comments

Comments
 (0)