Skip to content

Commit 95ef6ab

Browse files
committed
Merge pull request #11 from rust-embedded/clippy
Add Clippy Lint Tool and Fix Warnings
2 parents 254631a + d5e8880 commit 95ef6ab

File tree

6 files changed

+87
-43
lines changed

6 files changed

+87
-43
lines changed

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,10 @@ rust:
99

1010
script:
1111
- cargo test
12+
- |
13+
if [ "${TRAVIS_RUST_VERSION}" = "nightly" ]; then
14+
cargo install --git https://github.com/arcnmx/cargo-clippy
15+
cargo clippy --lib --features lints
16+
cargo clippy --bin gpio --features lints
17+
fi
1218
- cargo doc

Cargo.lock

Lines changed: 45 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ glob = "0.2"
1212
log = "0.3"
1313
env_logger = "0.3"
1414

15+
[dependencies.clippy]
16+
version = "*"
17+
git = "https://github.com/Manishearth/rust-clippy"
18+
rev = "master"
19+
optional = true
20+
21+
[features]
22+
default = []
23+
lints = ["clippy"]
24+
1525
[[bin]]
1626
name = "gpio"
1727
path = "src/main.rs"

src/config.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::fs::{self, File};
1414
use std::io;
1515
use std::io::prelude::*;
1616
use std::path::Path;
17+
use std::str::FromStr;
1718
use sysfs_gpio;
1819
use toml;
1920

@@ -116,7 +117,7 @@ impl Decodable for PinConfig {
116117
}
117118
})
118119
.unwrap_or(sysfs_gpio::Direction::In), // default: In
119-
names: d.read_struct_field("names", 0, Decodable::decode).unwrap_or(BTreeSet::new()),
120+
names: d.read_struct_field("names", 0, Decodable::decode).ok().unwrap_or_default(),
120121
export: d.read_struct_field("export", 0, Decodable::decode).unwrap_or(true),
121122
active_low: d.read_struct_field("active_low", 0, Decodable::decode).unwrap_or(false),
122123
})
@@ -130,6 +131,23 @@ impl PinConfig {
130131
}
131132
}
132133

134+
impl FromStr for GpioConfig {
135+
type Err = Error;
136+
137+
/// Load a GPIO configuration for the provided toml string
138+
fn from_str(config: &str) -> Result<Self, Error> {
139+
let mut parser = toml::Parser::new(config);
140+
let root = try!(parser.parse().ok_or(parser.errors));
141+
match Self::decode(&mut toml::Decoder::new(toml::Value::Table(root))) {
142+
Ok(cfg) => {
143+
try!(cfg.validate().or_else(|e| Err(Error::from(e))));
144+
Ok(cfg)
145+
}
146+
Err(e) => Err(Error::from(e)),
147+
}
148+
}
149+
}
150+
133151
impl GpioConfig {
134152
/// Validate invariants on the config that cannot easily be done earlier
135153
///
@@ -188,7 +206,7 @@ impl GpioConfig {
188206
config_instances.push(try!(Self::from_file(fragment)));
189207
}
190208

191-
if config_instances.len() == 0 {
209+
if config_instances.is_empty() {
192210
Err(Error::NoConfigFound)
193211
} else {
194212
let mut cfg = config_instances.remove(0);
@@ -199,19 +217,6 @@ impl GpioConfig {
199217
}
200218
}
201219

202-
/// Load a GPIO configuration for the provided toml string
203-
pub fn from_str(config: &str) -> Result<GpioConfig, Error> {
204-
let mut parser = toml::Parser::new(config);
205-
let root = try!(parser.parse().ok_or(parser.errors));
206-
match GpioConfig::decode(&mut toml::Decoder::new(toml::Value::Table(root))) {
207-
Ok(cfg) => {
208-
try!(cfg.validate().or_else(|e| Err(Error::from(e))));
209-
Ok(cfg)
210-
}
211-
Err(e) => Err(Error::from(e)),
212-
}
213-
}
214-
215220
/// Load a GPIO config from the specified path
216221
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<GpioConfig, Error> {
217222
let mut contents = String::new();
@@ -286,6 +291,7 @@ mod test {
286291
use std::iter::FromIterator;
287292
use std::collections::BTreeSet;
288293
use sysfs_gpio::Direction as D;
294+
use std::str::FromStr;
289295

290296
const BASIC_CFG: &'static str = r#"
291297
[[pins]]

src/export.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use sysfs_gpio;
1919
/// actions:
2020
///
2121
/// 1. For each GPIO name/alias, the corresponding symlink is remvoed from
22-
/// `/var/run/gpio/<name>` (or an alternate configured symlink_root).
22+
/// `/var/run/gpio/<name>` (or an alternate configured `symlink_root`).
2323
/// 2. The GPIO pin istself is unexported (vai /sys/class/gpio/unexport)
2424
///
2525
/// If the GPIO was already unexported, this function will continue

src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ fn main() {
136136

137137
// process global options
138138
let gpio_options = GpioOptions {
139-
configs: matches.values_of_lossy("config").unwrap_or(Vec::new()),
139+
configs: matches.values_of_lossy("config").unwrap_or_default(),
140140
};
141141

142142
// parse the config
@@ -213,21 +213,21 @@ fn main() {
213213
let unexport_options = GpioUnexportOptions {
214214
gpio_opts: gpio_options,
215215
pin: String::from(m.value_of("pin").unwrap()),
216-
symlink_root: m.value_of("symlink-root").map(|slr| String::from(slr)),
216+
symlink_root: m.value_of("symlink-root").map(String::from),
217217
};
218218
gpio_unexport::main(&cfg, &unexport_options);
219219
}
220220
("unexport-all", Some(m)) => {
221221
let unexportall_options = GpioUnexportAllOptions {
222222
gpio_opts: gpio_options,
223-
symlink_root: m.value_of("symlink-root").map(|slr| String::from(slr)),
223+
symlink_root: m.value_of("symlink-root").map(String::from),
224224
};
225225
gpio_unexportall::main(&cfg, &unexportall_options);
226226
}
227227
("status", Some(m)) => {
228228
let status_options = GpioStatusOptions {
229229
gpio_opts: gpio_options,
230-
pin: m.value_of("pin").map(|pin| String::from(pin)),
230+
pin: m.value_of("pin").map(String::from),
231231
};
232232
gpio_status::main(&cfg, &status_options);
233233
}

0 commit comments

Comments
 (0)